

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB find() to operate on recursive search?
Use find() with dot notation to perform recursive search. Let us first create a collection with documents −
> db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":101,"ClientName":"Chris"},{"ClientId":102,"ClientName":"Robert"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9a118b50a6c6dd317ad99") } > db.findOperationDemo.insertOne({"ClientDetails":[{"ClientId":110,"ClientName":"David"},{"ClientId":112,"ClientName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9a12fb50a6c6dd317ad9a") }
Following is the query to display all documents from a collection with the help of find() method −
> db.findOperationDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5cd9a118b50a6c6dd317ad99"), "ClientDetails" : [ { "ClientId" : 101, "ClientName" : "Chris" }, { "ClientId" : 102, "ClientName" : "Robert" } ] } { "_id" : ObjectId("5cd9a12fb50a6c6dd317ad9a"), "ClientDetails" : [ { "ClientId" : 110, "ClientName" : "David" }, { "ClientId" : 112, "ClientName" : "Mike" } ] }
Following is the query to implement find() for recursive search using dot notation −
> db.findOperationDemo.find({"ClientDetails.ClientId":110});
This will produce the following output −
{ "_id" : ObjectId("5cd9a12fb50a6c6dd317ad9a"), "ClientDetails" : [ { "ClientId" : 110, "ClientName" : "David" }, { "ClientId" : 112, "ClientName" : "Mike" } ] }
- Related Questions & Answers
- How to operate on all databases from the MongoDB shell?
- MongoDB Regex Search on Integer Value?
- Search a sub-field on MongoDB?
- Java Program for Binary Search (Recursive)
- Recursive function to do substring search in C++
- Using find() to search for nested keys in MongoDB?
- C Program for Binary Search (Recursive and Iterative)?
- Binary Search (Recursive and Iterative) in C Program
- Find MongoDB records based on a condition?
- Implement Text search in MongoDB
- Perform MongoDB full text search
- How to write recursive Python Function to find factorial?
- Recursive program to linearly search an element in a given array in C++
- How to fire find query on sub-documents in MongoDB?
- How to search for documents based on the value of adding two properties in MongoDB?
Advertisements