
- 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
Aggregate a $slice to get an element in exact position from a nested array in MongoDB?
You can use aggregate framework for this. Let us first create a collection with documents −
>db.exactPositionDemo.insertOne({"StudentName":"John","StudentScores":[78,98,56,45,89]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd29a1c345990cee87fd883") }
Following is the query to display all documents from a collection with the help of find() method −
> db.exactPositionDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd29a1c345990cee87fd883"), "StudentName" : "John", "StudentScores" : [ 78, 98, 56, 45, 89 ] }
Case 1 − Query to aggregate a $slice to get an element in exact position with 0,1 −
> db.exactPositionDemo.aggregate([ { "$project": { "StudentScores": { "$slice": ["$StudentScores",0,1] } }} ]);
This will produce the following output −
{ "_id" : ObjectId("5cd29a1c345990cee87fd883"), "StudentScores" : [ 78 ] }
Case 2 − Query to aggregate a $slice to get an element in exact position with 1,1 −
> db.exactPositionDemo.aggregate([ { "$project": { "StudentScores": { "$slice": ["$StudentScores",1,1] } }} ]);
This will produce the following output −
{ "_id" : ObjectId("5cd29a1c345990cee87fd883"), "StudentScores" : [ 98 ] }
- Related Articles
- MongoDB aggregate $slice to get the length of the array
- MongoDB query to aggregate nested array
- How to remove an element from a doubly-nested array in a MongoDB document?
- Extract a particular element from a nested array in MongoDB
- Get the first element in an array and return using MongoDB Aggregate?
- How to use $slice operator to get last element of array in MongoDB?
- Update field in exact element array in MongoDB?
- Extract particular element in MongoDB within a Nested Array?
- How to get a particular element from MongoDB array?
- MongoDB Aggregate Second Element from Input Element?
- MongoDB Aggregate to get average from document and of array elements?
- Golang program to remove an element from a slice
- Unset an attribute from a single array element in MongoDB?
- MongoDB query to slice only one element of array
- Removing an array element from a MongoDB collection

Advertisements