
- 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
Fetch specific multiple documents in MongoDB
To fetch specific multiple documents in MongoDB, use $in. Let us create a collection with documents −
> db.demo593.insertOne({id:1,"Name":"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e93177dfd2d90c177b5bcd9") } > db.demo593.insertOne({id:2,"Name":"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e931785fd2d90c177b5bcda") } > db.demo593.insertOne({id:3,"Name":"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e93178cfd2d90c177b5bcdb") } > db.demo593.insertOne({id:4,"Name":"Sam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e931792fd2d90c177b5bcdc") }
Display all documents from a collection with the help of find() method −
> db.demo593.find();
This will produce the following output −
{ "_id" : ObjectId("5e93177dfd2d90c177b5bcd9"), "id" : 1, "Name" : "Chris" } { "_id" : ObjectId("5e931785fd2d90c177b5bcda"), "id" : 2, "Name" : "John" } { "_id" : ObjectId("5e93178cfd2d90c177b5bcdb"), "id" : 3, "Name" : "Bob" } { "_id" : ObjectId("5e931792fd2d90c177b5bcdc"), "id" : 4, "Name" : "Sam" }
Following is the query to fetch specific multiple documents −
> db.demo593.find({id:{$in:[1,3]}});
This will produce the following output −
{ "_id" : ObjectId("5e93177dfd2d90c177b5bcd9"), "id" : 1, "Name" : "Chris" } { "_id" : ObjectId("5e93178cfd2d90c177b5bcdb"), "id" : 3, "Name" : "Bob" }
- Related Articles
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB query to implement nor query to fetch documents except a specific document
- Fetch specific documents with array values in MongoD
- MongoDB compound conditions to fetch documents?
- Match MongoDB documents with field value greater than a specific number and fetch them?
- Fetch specific field values in MongoDB
- Fetch all the ids of MongoDB documents?
- MongoDB to fetch documents with $or Operator
- Search for multiple documents in MongoDB?
- MongoDB filter multiple sub-documents?
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- How to merge multiple documents in MongoDB?
- How to update multiple documents in MongoDB?
- MongoDB query to add multiple documents

Advertisements