
- 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 do I access subdocuments in MongoDB queries?
To access subdocuments in MongoDB, use find() with dot notation. Let us create a collection with documents −
> db.demo670.insertOne({ ... id:101, ... "details": ... { ... Name:"Chris", ... Age:21, ... CountryName:"US", ... SubjectName:"MongoDB" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5ea3e31d04263e90dac943de") } > db.demo670.insertOne({ id:102, "details": { Name:"David", Age:22, CountryName:"UK", SubjectName:"MySQL" } } ); { "acknowledged" : true, "insertedId" : ObjectId("5ea3e33604263e90dac943df") }
Display all documents from a collection with the help of find() method −
> db.demo670.find();
This will produce the following output −
{ "_id" : ObjectId("5ea3e31d04263e90dac943de"), "id" : 101, "details" : { "Name" : "Chris", "Age" : 21, "CountryName" : "US", "SubjectName" : "MongoDB" } } { "_id" : ObjectId("5ea3e33604263e90dac943df"), "id" : 102, "details" : { "Name" : "David", "Age" : 22, "CountryName" : "UK", "SubjectName" : "MySQL" } }
Following is the query to access subdocuments −
> db.demo670.find({"details.SubjectName":"MongoDB"},{"details.CountryName":1});7
This will produce the following output −
{ "_id" : ObjectId("5ea3e31d04263e90dac943de"), "details" : { "CountryName" : "US" } }
- Related Articles
- How do I make case-insensitive queries on MongoDB?
- Manipulating subdocuments in MongoDB
- How to keep appending subdocuments in MongoDB?
- How to update array of subdocuments in MongoDB?
- Query array of subdocuments in MongoDB
- MongoDB - how can I access fields in a document?
- MongoDB query to sort subdocuments
- How do I sort natural in MongoDB?
- Split a document by its subdocuments in MongoDB
- How do I access values created by serializeArray in jQuery?
- How do I access the serial (RS232) port in Python?
- How to use arrays as filters by querying subdocuments in MongoDB?
- How do I access SQLite database instance on iPhone
- How do I query a MongoDB collection?
- Get distinct pair of objects with all subdocuments in MongoDB?

Advertisements