
- 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
Search a sub-field on MongoDB?
To search a sub-filed in MongoDB, you can use double quotes along with dot notation. Let us first create a collection with documents −
> db.searchSubFieldDemo.insertOne( ... { ... "UserDetails": ... {"UserEmailId":"John123@gmail.com","UserAge":21} ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd3d909edc6604c74817ce2") } > db.searchSubFieldDemo.insertOne( { "UserDetails": {"UserEmailId":"Carol@yahoo.com","UserAge":26} } ); { "acknowledged" : true, "insertedId" : ObjectId("5cd3d9a4edc6604c74817ce3") }
Following is the query to display all documents from a collection with the help of find() method −
> db.searchSubFieldDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd3d909edc6604c74817ce2"), "UserDetails" : { "UserEmailId" : "John123@gmail.com", "UserAge" : 21 } } { "_id" : ObjectId("5cd3d9a4edc6604c74817ce3"), "UserDetails" : { "UserEmailId" : "Carol@yahoo.com", "UserAge" : 26 } }
Following is the query to search a sub-field on MongoDB −
> db.searchSubFieldDemo.find({"UserDetails.UserEmailId":"Carol@yahoo.com"});
This will produce the following output −
{ "_id" : ObjectId("5cd3d9a4edc6604c74817ce3"), "UserDetails" : { "UserEmailId" : "Carol@yahoo.com", "UserAge" : 26 } }
- Related Articles
- MongoDB query by sub-field?
- Sum MongoDB Sub-documents field?
- Search by specific field in MongoDB
- How to sum every field in a sub document of MongoDB?
- How to add an extra field in a sub document in MongoDB?
- How to search for string or number in a field with MongoDB?
- MongoDB Regex Search on Integer Value?
- How to get distinct list of sub-document field values in MongoDB?
- Creating an index on a nested MongoDB field?
- How to search for a record (field) and then delete it in MongoDB?
- MongoDB find() to operate on recursive search?
- How to fire find query on sub-documents in MongoDB?
- MongoDB query to search for string like “@email” in the field values
- Filter sub documents by sub document in MongoDB?
- How to add a sub-document to sub-document array in MongoDB?

Advertisements