
- 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
MongoDB Aggregation to slice array inside array
For this, use aggregate() in MongoDB. In that, use $slice to slice array inside array. Let us create a collection with documents −
> db.demo111.insertOne( ... { ... "_id" : 101, ... "Name" : "Chris", ... "Details" : [ ... { ... "_id" : 101, ... "Score" : 78, ... "Subjects" : [ ... { ... "_id" : "10001", ... "SubjectName" : "MySQL" ... }, ... { ... "_id" : "10003", ... "SubjectName" : "MongoDB" ... } ... ] ... }, ... { ... "_id" : 102, ... "Score" : 87, ... "Subjects" : [ ... { ... "_id" : "10004", ... "SubjectName" : "Java" ... } ... ] ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : 101 }
Display all documents from a collection with the help of find() method −
> db.demo111.find();
This will produce the following output −
{ "_id" : 101, "Name" : "Chris", "Details" : [ { "_id" : 101, "Score" : 78, "Subjects" : [ { "_id" : "10001", "SubjectName" : "MySQL" }, { "_id" : "10003", "SubjectName" : "MongoDB" } ] }, { "_id" : 102, "Score" : 87, "Subjects" : [ { "_id" : "10004", "SubjectName" : "Java" } ] } ] }
Following is the query to slice array inside array −
> db.demo111.aggregate([ ... { "$addFields": { ... "Details": { ... "$map": { ... "input": "$Details", ... "as": "out", ... "in": { ... "_id": "$$out._id", ... "Score": "$$out.Score", ... "Subjects": { "$slice": [ "$$out.Subjects", 1 ] } ... } ... } ... } ... }} ... ])
This will produce the following output −
{ "_id" : 101, "Name" : "Chris", "Details" : [ { "_id" : 101, "Score" : 78, "Subjects" : [ { "_id" : "10001", "SubjectName" : "MySQL" } ] }, { "_id" : 102, "Score" : 87, "Subjects" : [ { "_id" : "10004", "SubjectName" : "Java" } ] } ] }
- Related Articles
- MongoDB aggregation with equality inside array?
- MongoDB slice array in populated field?
- MongoDB query to slice only one element of array
- MongoDB aggregation framework to sort by length of array?
- MongoDB aggregation group and remove duplicate array values?
- MongoDB aggregate $slice to get the length of the array
- MongoDB query to get average in aggregation of array element?
- MongoDB Increment value inside nested array?
- JavaScript Array slice()
- Apply a condition inside subset in MongoDB Aggregation?
- Increment MongoDB value inside a nested array
- Get array items inside a MongoDB document?
- Update elements inside an array in MongoDB?
- How to use $slice operator to get last element of array in MongoDB?
- Find sum of fields inside array in MongoDB?

Advertisements