
- 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 query by sub-field?
You can use dot(.) notation to query by subfield. Let us create a collection with a document. The query to create a collection with a document is as follows −
> db.queryBySubFieldDemo.insertOne( ... { ... "StudentPersonalDetails" : {"StudentName" : "John","StudentHobby" :"Photography"}, ... "StudentScores" : {"MathScore" : 56} ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c92c2995259fcd195499808") } > db.queryBySubFieldDemo.insertOne( ... { ... "StudentPersonalDetails" : {"StudentName" : "Chris","StudentHobby" :"Reading"}, ... "StudentScores" : {"MathScore" : 97} ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5c92c2df5259fcd195499809") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.queryBySubFieldDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c92c2995259fcd195499808"), "StudentPersonalDetails" : { "StudentName" : "John", "StudentHobby" : "Photography" }, "StudentScores" : { "MathScore" : 56 } } { "_id" : ObjectId("5c92c2df5259fcd195499809"), "StudentPersonalDetails" : { "StudentName" : "Chris", "StudentHobby" : "Reading" }, "StudentScores" : { "MathScore" : 97 } }
Here is the query by subfield −
> db.queryBySubFieldDemo.find({"StudentPersonalDetails.StudentName":"Chris"}).pretty();
The following is the output −
{ "_id" : ObjectId("5c92c2df5259fcd195499809"), "StudentPersonalDetails" : { "StudentName" : "Chris", "StudentHobby" : "Reading" }, "StudentScores" : { "MathScore" : 97 } }
- Related Articles
- Sum MongoDB Sub-documents field?
- Search a sub-field on MongoDB?
- Filter sub documents by sub document in MongoDB?
- MongoDB query for a single field
- MongoDB query for capped sub-collection in an array
- MongoDB query for counting number of unique fields grouped by another field?
- MongoDB query to set a sub item in an array?
- MongoDB query to remove element from array as sub property
- Is there a MongoDB query to concatenate deep sub-lists?
- How to fire find query on sub-documents in MongoDB?
- How to sum every field in a sub document of MongoDB?
- MongoDB Query for boolean field as “not true”
- How to query on list field in MongoDB?
- MongoDB query to update array with another field?
- Field selection within MongoDB query using dot notation?

Advertisements