
- 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
Find the MongoDB document from sub array?
You can use dot(.) notation to find the document from sub array. Let us first create a collection with documents −
> db.findDocumentDemo.insertOne( ... { ... "EmployeeDetails" : ... { ... "EmployeeAppraisalTime": ... ... [ ... ... {"EmployeeDesignation": "Developer", "Salary": 45000}, ... {"EmployeeDesignation": "Tester", "Salary": 30000}, ... {"EmployeeDesignation": "HR", "Salary": 22000}, ... {"EmployeeDesignation": "Accountant", "Salary": 18000} ... ] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2c0f7b64f4b851c3a13a8") } > db.findDocumentDemo.insertOne( ... { ... "EmployeeDetails" : ... { ... "EmployeeAppraisalTime": ... ... [ ... ... {"EmployeeDesignation": "Developer", "Salary": 105000}, ... {"EmployeeDesignation": "Tester", "Salary": 45000}, ... {"EmployeeDesignation": "HR", "Salary": 34000}, ... {"EmployeeDesignation": "Accountant", "Salary": 24000} ... ] ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cd2c1d5b64f4b851c3a13a9") }
Following is the query to display all documents from a collection with the help of find() method −
> db.findDocumentDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd2c0f7b64f4b851c3a13a8"), "EmployeeDetails" : { "EmployeeAppraisalTime" : [ { "EmployeeDesignation" : "Developer", "Salary" : 45000 }, { "EmployeeDesignation" : "Tester", "Salary" : 30000 }, { "EmployeeDesignation" : "HR", "Salary" : 22000 }, { "EmployeeDesignation" : "Accountant", "Salary" : 18000 } ] } } { "_id" : ObjectId("5cd2c1d5b64f4b851c3a13a9"), "EmployeeDetails" : { "EmployeeAppraisalTime" : [ { "EmployeeDesignation" : "Developer", "Salary" : 105000 }, { "EmployeeDesignation" : "Tester", "Salary" : 45000 }, { "EmployeeDesignation" : "HR", "Salary" : 34000 }, { "EmployeeDesignation" : "Accountant", "Salary" : 24000 } ] } }
Following is the query to find document from sub array −
> db.findDocumentDemo.find({ 'EmployeeDetails.EmployeeAppraisalTime.EmployeeDesignation': 'Developer', 'EmployeeDetails.EmployeeAppraisalTime.Salary': { '$in': [45000,105000] } } );
This will produce the following output −
{ "_id" : ObjectId("5cd2c0f7b64f4b851c3a13a8"), "EmployeeDetails" : { "EmployeeAppraisalTime" : [ { "EmployeeDesignation" : "Developer", "Salary" : 45000 }, { "EmployeeDesignation" : "Tester", "Salary" : 30000 }, { "EmployeeDesignation" : "HR", "Salary" : 22000 }, { "EmployeeDesignation" : "Accountant", "Salary" : 18000 } ] } } { "_id" : ObjectId("5cd2c1d5b64f4b851c3a13a9"), "EmployeeDetails" : { "EmployeeAppraisalTime" : [ { "EmployeeDesignation" : "Developer", "Salary" : 105000 }, { "EmployeeDesignation" : "Tester", "Salary" : 45000 }, { "EmployeeDesignation" : "HR", "Salary" : 34000 }, { "EmployeeDesignation" : "Accountant", "Salary" : 24000 } ] } }
- Related Articles
- How to add a sub-document to sub-document array in MongoDB?
- Working with MongoDB $sort for sub array document
- Filter sub documents by sub document in MongoDB?
- MongoDB query to remove array elements from a document?
- MongoDB query to remove element from array as sub property
- Identify last document from MongoDB find() result set?
- Find document that matches same array elements in MongoDB?
- Pull an element in sub of sub-array in MongoDB?
- How to sum every field in a sub document of MongoDB?
- Find document in MongoDB where at least one item from an array is not in the other?
- How do I delete array value from a document in MongoDB?
- MongoDB Aggregate to get average from document and of array elements?
- How to get distinct list of sub-document field values in MongoDB?
- How to add an extra field in a sub document in MongoDB?
- Find document with array that contains a specific value in MongoDB

Advertisements