
- 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 and projection?
For this, use $project along with aggregate(). The $project in aggregation passes along the documents with the requested fields to the next stage in the pipeline.
Let us create a collection with documents −
> db.demo762.insertOne({ ... "_id" : { ... "userId":101, ... "userName":"Chris" ... }, .. . "countryName" : "US", ... ... "details" : [ ... { ... "Name" : "Robert", ... "DueDate" : "2020-04-10" ... ... }, ... ... { ... "Name" : "Robert", ... "DueDate" : "2020-04-09" ... }, ... { ... "Name" : "Robert", ... "DueDate" : "2020-03-06" ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : { "userId" : 101, "userName" : "Chris" } }
Display all documents from a collection with the help of find() method −
> db.demo762.find();
This will produce the following output −
{ "_id" : { "userId" : 101, "userName" : "Chris" }, "countryName" : "US", "details" : [ { "Name" : "Robert", "DueDate" : "2020-04-10" }, { "Name" : "Robert", "DueDate" : "2020-04-09" }, { "Name" : "Robert", "DueDate" : "2020-03-06" } ] }
Following is the query for MongoDB aggregation and projection −
> db.demo762.aggregate([ ... { "$match": { ... "_id": { "$eq": { userId:101,userName:"Chris" }} ... }}, ... { "$unwind": "$details" }, ... { "$sort": { "details.DueDate": 1 }}, ... { "$group": { ... "_id": "$_id", ... "details": { "$push": "$details" }, ... "countryName": { "$first": "$countryName" } ... }}, ... { "$project": { "details": { "$slice": ["$details", 2] } ,"countryName": 1 }} ... ]).pretty();
This will produce the following output −
{ "_id" : { "userId" : 101, "userName" : "Chris" }, "countryName" : "US", "details" : [ { "Name" : "Robert", "DueDate" : "2020-03-06" }, { "Name" : "Robert", "DueDate" : "2020-04-09" } ] }
- Related Articles
- MongoDB multidimensional array projection?
- MongoDB Limit fields and slice projection together?
- MongoDB projection on specific nested properties?
- Projection of one column in MongoDB?
- MongoDB divide aggregation operator?
- Sort and Group in one MongoDB aggregation query?
- MongoDB aggregation group and remove duplicate array values?
- Perform aggregation sort in MongoDB?
- MongoDB aggregation with multiple keys
- Aggregation in MongoDB for nested documents?
- Perform min/max with MongoDB aggregation
- MongoDB aggregation with equality inside array?
- MongoDB projection result as an array of selected items?
- MongoDB aggregation to combine or merge fields and then count?
- MongoDB aggregation framework match OR is possible?

Advertisements