
- 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
How to select a specific subdocument in MongoDB?
To select a specific subdocument in MongoDB, use find(). Let us create a collection with documents −
> db.demo37.insertOne({"Details":[{"Name":"Chris","Age":21},{"Name":"David","Age":23}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e176635cfb11e5c34d898d7") } > db.demo37.insertOne({"Details":[{"Name":"Sam","Age":23},{"Name":"Robert","Age":25}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e17664acfb11e5c34d898d8") }
Display all documents from a collection with the help of find() method −
> db.demo37.find();
This will produce the following output −
{ "_id" : ObjectId("5e176635cfb11e5c34d898d7"), "Details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "David", "Age" : 23 } ] } { "_id" : ObjectId("5e17664acfb11e5c34d898d8"), "Details" : [ { "Name" : "Sam", "Age" : 23 }, { "Name" : "Robert", "Age" : 25 } ] }
Following is the query to select subdocument −
> db.demo37.find({'Details.Name' : 'Sam'},{_id: 0, 'Details.$.Name': 1});
This will produce the following output −
{ "Details" : [ { "Name" : "Sam", "Age" : 23 } ] }
- Related Articles
- How to sort, select and query subdocument in MongoDB?
- Finding a specific item in subdocument with MongoDB?
- How to select specific columns in MongoDB query?
- How to filter array in subdocument with MongoDB?
- How to select MongoDB document that does not consist a specific field?
- Sort by subdocument in MongoDB
- How to select objects where an array contains only a specific field in MongoDB?
- How to query documents by a condition on the subdocument in MongoDB?
- MongoDB to sort by subdocument match?
- How to access subdocument value when the key is a number in MongoDB?
- MongoDB query to remove subdocument from document?
- Query MongoDB subdocument to print on one line?
- How to select a single field in MongoDB?
- MongoDB query to find and return subdocument with criteria?
- MongoDB query select and display only a specific field from the document?

Advertisements