
- 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
Query an array in MongoDB to fetch a specific value
To fetch a specific value from an array, use aggregate() along with $project. Let us create a collection with documents −
> db.demo761.insertOne( ... { ... "details": [ ... { ... "student": { ... "FullName": "Chris Brown" ... } ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb034d15637cd592b2a4af1") } > db.demo761.insertOne( ... { ... "details": [ ... { ... "student": { ... "FullName": "Chris Taylor" ... } ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5eb034d25637cd592b2a4af2") }
Display all documents from a collection with the help of find() method −
> db.demo761.find();
This will produce the following output −
{ "_id" : ObjectId("5eb034d15637cd592b2a4af1"), "details" : [ { "student" : { "FullName" : "Chris Brown" } } ] } { "_id" : ObjectId("5eb034d25637cd592b2a4af2"), "details" : [ { "student" : { "FullName" : "Chris Taylor" } } ] }
Following is how to query an array in MongoDB −
> db.demo761.aggregate([ ... {'$unwind':'$details'}, ... {'$match': { ... 'details.student.FullName':'Chris Taylor', ... }}, ... {'$project': { ... 'details.student.FullName': 1, '_id': 0 ... }}, ... ])
This will produce the following output −
{ "details" : { "student" : { "FullName" : "Chris Taylor" } } }
- Related Articles
- How to query MongoDB a value with $lte, $in and $not to fetch specific values?
- MongoDB query to fetch array values
- MongoDB query to implement nor query to fetch documents except a specific document
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB query to replace value in an array?
- MongoDB query to return specific fields from an array?
- Fetch a specific document in MongoDB with array elements
- MongoDB aggregation to fetch documents with specific field value?
- Accessing array values in a MongoDB collection to fetch a specific document
- MongoDB query to match documents with array values greater than a specific value
- MySQL query to fetch specific records matched from an array (comma separated values)
- MongoDB query to pull a specific value from a document
- MongoDB query to fetch random value using Map Reduce concept.
- MongoDB query for documents whose array elements does not have a specific value
- MongoDB query to increment a specific value using custom variable

Advertisements