- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Search for multiple documents in MongoDB?
To search for multiple documents in MongoDB, use $in. Let us create a collection with documents −
> db.demo161.insertOne({"ClientId":101,"ClientName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3577cafdf09dd6d0853a09") } > db.demo161.insertOne({"ClientId":102,"ClientName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3577d0fdf09dd6d0853a0a") } > db.demo161.insertOne({"ClientId":103,"ClientName":"David","ClientAge":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e3577dffdf09dd6d0853a0b") } > db.demo161.insertOne({"ClientId":104,"ClientName":"Carol","ClientAge":31}); { "acknowledged" : true, "insertedId" : ObjectId("5e3577eefdf09dd6d0853a0c") }
Display all documents from a collection with the help of find() method −
> db.demo161.find();
This will produce the following output −
{ "_id" : ObjectId("5e3577cafdf09dd6d0853a09"), "ClientId" : 101, "ClientName" : "Chris" } { "_id" : ObjectId("5e3577d0fdf09dd6d0853a0a"), "ClientId" : 102, "ClientName" : "David" } { "_id" : ObjectId("5e3577dffdf09dd6d0853a0b"), "ClientId" : 103, "ClientName" : "David", "ClientAge" : 35 } { "_id" : ObjectId("5e3577eefdf09dd6d0853a0c"), "ClientId" : 104, "ClientName" : "Carol", "ClientAge" : 31 }
Following is the query to search for multiple documents in MongoDB −
> db.demo161.find({ClientId:{$in:[101,103,104]}});
This will produce the following output −
{ "_id" : ObjectId("5e3577cafdf09dd6d0853a09"), "ClientId" : 101, "ClientName" : "Chris" } { "_id" : ObjectId("5e3577dffdf09dd6d0853a0b"), "ClientId" : 103, "ClientName" : "David", "ClientAge" : 35 } { "_id" : ObjectId("5e3577eefdf09dd6d0853a0c"), "ClientId" : 104, "ClientName" : "Carol", "ClientAge" : 31 }
- Related Articles
- Search multiple fields for multiple values in MongoDB?
- Search for documents matching first item in an array with MongoDB?
- Fetch specific multiple documents in MongoDB
- MongoDB filter multiple sub-documents?
- How to merge multiple documents in MongoDB?
- How to update multiple documents in MongoDB?
- MongoDB query to add multiple documents
- Search for documents with similar arrays in MongoDB and order by similarity value
- Check for existing documents/embedded documents in MongoDB
- Fetch multiple documents in MongoDB query with OR condition?
- How to delete multiple documents in MongoDB using deleteMany()?
- How to search documents through an integer array in MongoDB?
- How to search for documents based on the value of adding two properties in MongoDB?
- Aggregation in MongoDB for nested documents?
- Update values in multiple documents with multi parameter in MongoDB?

Advertisements