
- 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 you find a MongoDB record that is two level deep?
To find a MongoDB record that is two level deep, loop inside MongoDB $where. Let us create a collection with documents −
> db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ... "StudentName" : "Chris", ... "StudentAge" : 23 ... }, ... "SecondPosition" : { ... "StudentName" : "David", ... "StudentAge" : 20 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e804e2fb0f3fa88e2279069") } > db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ... "StudentName" : "Carol", ... "StudentAge" : 21 ... }, ... "SecondPosition" : { ... "StudentName" : "John", ... "StudentAge" : 22 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e804fb0b0f3fa88e227906a") }
Display all documents from a collection with the help of find() method −
> db.demo468.find();
This will produce the following output −
{ "_id" : ObjectId("5e804e2fb0f3fa88e2279069"), "FirstPosition" : { "StudentName" : "Chris", "StudentAge" : 23 }, "SecondPosition" : { "StudentName" : "David", "StudentAge" : 20 } } { "_id" : ObjectId("5e804fb0b0f3fa88e227906a"), "FirstPosition" : { "StudentName" : "Carol", "StudentAge" : 21 }, "SecondPosition" : { "StudentName" : "John", "StudentAge" : 22 } }
Following is the query to find a MongoDB record that is two level deep −
> db.demo468.find({ ... $where: function() { ... for (var i in this) { ... if (this[i]["StudentName"] == "John") { ... return true; ... } ... } ... return false; ... } ... })
This will produce the following output −
{ "_id" : ObjectId("5e804fb0b0f3fa88e227906a"), "FirstPosition" : { "StudentName" : "Carol", "StudentAge" : 21 }, "SecondPosition" : { "StudentName" : "John", "StudentAge" : 22 } }
- Related Articles
- How do you do a deep copy of an object in .NET?
- How do I add a field to existing record in MongoDB?
- How to find a record by _id in MongoDB?
- How do I find all documents with a field that is NaN in MongoDB?
- How do you test if two external values are equal in a MongoDB criteria object?
- How can we update a record in MongoDB?
- How do I use MongoDB to count only collections that match two fields?
- Why is MongoDB taking too much time to find the record?
- MongoDB query to find a specific city record from a collection
- How do you check that a number is NaN in JavaScript?
- Find items that do not have a certain field in MongoDB?
- How do group (two-level) axis labels in a chart in Excel?
- How do I select data that does not have a null record in MySQL?
- Is there a MongoDB query to concatenate deep sub-lists?
- How do you compare two fields in MongoDB while performing an operation on one of them?

Advertisements