
- 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
Using aggregation pipeline to fetch records in MongoDB
The MongoDB aggregation pipeline has stages. Each stage transforms the documents as they pass through the pipeline.
Let us first create a collection with documents −
> db.demo218.insertOne({"Name":"Chris","Branch":"CS",Marks:[65,78,36,90]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e5f4903d395bdc2134712") } > db.demo218.insertOne({"Name":"David","Branch":"ME",Marks:[56,45,42,51]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e5f6203d395bdc2134713") } > db.demo218.insertOne({"Name":"Chris","Branch":"CS",Marks:[78,65,89]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3e5f6c03d395bdc2134714") }
Display all documents from a collection with the help of find() method −
> db.demo218.find();
This will produce the following output −
{ "_id" : ObjectId("5e3e5f4903d395bdc2134712"), "Name" : "Chris", "Branch" : "CS", "Marks" : [ 65, 78, 36, 90 ] } { "_id" : ObjectId("5e3e5f6203d395bdc2134713"), "Name" : "David", "Branch" : "ME", "Marks" : [ 56, 45, 42, 51 ] } { "_id" : ObjectId("5e3e5f6c03d395bdc2134714"), "Name" : "Chris", "Branch" : "CS", "Marks" : [ 78, 65, 89 ] }
Following is the query for aggregation pipeline −
> db.demo218.aggregate([ ... { "$unwind": "$Marks" }, ... { "$match": ... { ... "Branch": "CS", ... "Marks": { "$gt": 88 } ... } ... }, ... { "$group": ... { ... "_id": "$_id", ... "Branch": { "$first": "$Branch" }, ... "Marks": { "$first": "$Marks" } ... } ... } ...])
This will produce the following output −
{ "_id" : ObjectId("5e3e5f6c03d395bdc2134714"), "Branch" : "CS", "Marks" : 89 } { "_id" : ObjectId("5e3e5f4903d395bdc2134712"), "Branch" : "CS", "Marks" : 90 }
- Related Articles
- MongoDB aggregation to fetch documents with specific field value?
- Fetch records in MongoDB on querying its subset
- MongoDB query to fetch date records (ISODate format) in a range
- MongoDB query to group several fields using aggregation framework?
- How to use or operator in MongoDB to fetch records on the basis of existence?
- Fetch records from comma separated values using MySQL IN()?
- How to order records and fetch some using MySQL LIMIT?
- Using $addFields pipeline and run with the MongoDB $filter operator
- Fetch records from a subdocument array wherein id begins from 234 in MongoDB
- Fetch records on the basis of LastName using MySQL IN()
- Perform aggregation sort in MongoDB?
- How to calculate a sum of specific documents using MongoDB aggregation?
- MongoDB divide aggregation operator?
- MongoDB aggregation and projection?
- Is it possible to sum two fields in MongoDB using the Aggregation framework?

Advertisements