
- 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 fetch documents with specific field value?
For this, use aggregate(). Let’s say we have to fetch documents with a field “Age” with value “21”.
Let us now create a collection with documents −
> db.demo685.insertOne( ... { ... "details": ... [ ... { ... Name:"Chris", ... Age:21 ... }, ... { ... Name:"David", ... Age:23 ... }, ... { ... Name:"Bob", ... Age:21 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea54f6fa7e81adc6a0b395b") }
Display all documents from a collection with the help of find() method −
> db.demo685.find();
This will produce the following output −
{ "_id" : ObjectId("5ea54f6fa7e81adc6a0b395b"), "details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "David", "Age" : 23 }, { "Name" : "Bob", "Age" : 21 } ] }
Following is the query for MongoDB aggregation −
> db.demo685.aggregate([ { $unwind: "$details" }, { $match: { "details.Age": 21 } }, { $project: {_id: 0}} ]).pretty();
This will produce the following output −
{ "details" : { "Name" : "Chris", "Age" : 21 } } { "details" : { "Name" : "Bob", "Age" : 21 } }
- Related Articles
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- MongoDB query (aggregation framework) to match a specific field value
- Fetch specific multiple documents in MongoDB
- Fetch specific field values in MongoDB
- How to calculate a sum of specific documents using MongoDB aggregation?
- Find MongoDB documents that contains specific field?
- MongoDB to fetch documents with $or Operator
- Find documents that contains specific field in MongoDB?
- Fetch specific documents with array values in MongoD
- MongoDB aggregation to get two documents with the least marks
- MongoDB query to implement nor query to fetch documents except a specific document
- Find value above a specific value in MongoDB documents?
- Sort the MongoDB documents in ascending order with aggregation?
- MongoDB compound conditions to fetch documents?

Advertisements