
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Set MongoDB $slice with a range?
To set slice along with a range, use the $slice operator with parameters. These parameters are to be set for beginning position of the elements to be fetched and the 2nd parameter is for range. Let us create a collection with documents −
> db.demo54.insertOne({"ListOfValues":[100,2030,5353,7364,635,535,524,423,2434,1323,799874,90]}); { "acknowledged" : true, "insertedId" : ObjectId("5e27151ecfb11e5c34d89914") }
Display all documents from a collection with the help of find() method −
> db.demo54.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e27151ecfb11e5c34d89914"), "ListOfValues" : [ 100, 2030, 5353, 7364, 635, 535, 524, 423, 2434, 1323, 799874, 90 ] }
Following is the query to set slice with a range −
> db.demo54.find({}, { "ListOfValues": { $slice: [5,3]}}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5e27151ecfb11e5c34d89914"), "ListOfValues" : [ 535, 524, 423 ] }
- Related Articles
- Set multiple conditions in MongoDB and fetch value in a range
- MongoDB query to fetch elements between a range excluding both the numbers used to set range?
- MongoDB slice array in populated field?
- MongoDB Aggregation to slice array inside array
- MongoDB Limit fields and slice projection together?
- Push and slice multiple times in MongoDB?
- Is it possible to achieve a slice chain in MongoDB?
- Set MongoDB compound index with a fixed value field
- MongoDB query to slice only one element of array
- Python Count set bits in a range?
- MongoDB query to find value in array with multiple criteria (range)
- MongoDB aggregate $slice to get the length of the array
- Selecting records within a range and with a condition set on two columns in MySQL?
- Slice the border image with CSS
- Copy set bits in a range in C++

Advertisements