- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is it possible to achieve a slice chain in MongoDB?
Yes, you can achieve this using aggregate framework. Let us first create a collection with documents −
> db.sliceOfSliceDemo.insertOne( ... { ... "Name": "John", ... "Details": [["First 1:1", "First 1:2"],["second 2:1","Second 2:2"]] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ccf3fcfdceb9a92e6aa195a") }
Following is the query to display all documents from a collection with the help of find() method −
> db.sliceOfSliceDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ccf3fcfdceb9a92e6aa195a"), "Name" : "John", "Details" : [ [ "First 1:1", "First 1:2" ], [ "second 2:1", "Second 2:2" ] ] }
Following is the query to get a slice of a slice i.e. achieve a slice chain in MongoDB −
> db.sliceOfSliceDemo.aggregate([ {$match:{"Name":"John"}}, {$unwind:"$Details"}, {$unwind:"$Details"}, {$group:{"_id":"$Name","Details":{$last:"$Details"}}}, {$project:{"Name":"$_id","Details":1}} ]);
This will produce the following output −
{ "_id" : "John", "Details" : "Second 2:2", "Name" : "John" }
- Related Articles
- Is it possible to cast in a MongoDB Query?
- Is it possible to make a case-insensitive query in MongoDB?
- Is it possible to use MongoDB capped collection?
- Is it possible to exclude nested fields in MongoDB with a wildcard?
- Is it possible to write to MongoDB console in JavaScript execution?
- Is it possible to use MongoDB field value as a pattern in $regex?
- Achieve Pagination with MongoDB?
- Is it possible to return a list of specific values from a query in MongoDB?
- Is it possible to use MongoDB field value as pattern in $regex?
- Is it possible to return a list of specific values from a MongoDB query?
- Is it possible to rename _id field after MongoDB group aggregation?
- Is it possible to sum two fields in MongoDB using the Aggregation framework?
- Set MongoDB $slice with a range?
- What is connection pooling in C# and how to achieve it?
- MongoDB slice array in populated field?

Advertisements