
- 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 search according to fields in inner classes using MongoDB db.coll.find()?
Use dot notation(.) to search in inner classes using MongoDB. Let us first create a collection with documents −
> db.searchInInnerDemo.insertOne( ... { ... "StudentFirstName" : "Robert", ... "StudentTechnicalDetails": ... { ... "StudentBackEndTechnology" : "MongoDB", ... "StudentLanguage" : "Java" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2dd89b64f4b851c3a13d2") } > > db.searchInInnerDemo.insertOne( ... { ... "StudentFirstName" : "David", ... "StudentTechnicalDetails": ... { ... "StudentBackEndTechnology" : "MySQL", ... "StudentLanguage" : "PHP" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2dda3b64f4b851c3a13d3") }
Following is the query to display all documents from a collection with the help of find() method −
> db.searchInInnerDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2dd89b64f4b851c3a13d2"), "StudentFirstName" : "Robert", "StudentTechnicalDetails" : { "StudentBackEndTechnology" : "MongoDB", "StudentLanguage" : "Java" } } { "_id" : ObjectId("5cd2dda3b64f4b851c3a13d3"), "StudentFirstName" : "David", "StudentTechnicalDetails" : { "StudentBackEndTechnology" : "MySQL", "StudentLanguage" : "PHP" } }
Case 1 − Query to search in inner class to match only one property −
>db.searchInInnerDemo.find({"StudentTechnicalDetails.StudentBackEndTechnology":"MongoDB"}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2dd89b64f4b851c3a13d2"), "StudentFirstName" : "Robert", "StudentTechnicalDetails" : { "StudentBackEndTechnology" : "MongoDB", "StudentLanguage" : "Java" } }
Case 2 − Query to search the document with scanning full field name −
>db.searchInInnerDemo.find({"StudentTechnicalDetails":{"StudentBackEndTechnology":"MongoDB","StudentLanguage":"Java"}}).pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2dd89b64f4b851c3a13d2"), "StudentFirstName" : "Robert", "StudentTechnicalDetails" : { "StudentBackEndTechnology" : "MongoDB", "StudentLanguage" : "Java" } }
- Related Articles
- How do I work with array fields in MongoDB to match all?
- How do I index “or” in MongoDB for indexing multiple fields?
- Why do we need inner classes in Java?
- How do I use MongoDB to count only collections that match two fields?
- Search multiple fields for multiple values in MongoDB?
- How do I sort a multidimensional array by one of the fields of the inner array in PHP?
- MongoDB - how can I access fields in a document?
- How are anonymous (inner) classes used in Java?
- How to sort inner array in MongoDB?
- How to remove key fields in MongoDB?
- What are inner classes in Java?
- How do I search a list in Java?
- How do I sort natural in MongoDB?
- What are anonymous inner classes in Java?
- How can I update and increment two fields in one command in MongoDB?

Advertisements