
- 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 to find through list of ids in MongoDB?
You can use $in operator to find through the list of ids in MongoDB. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −
> db.findListOfIdsDemo.insertOne({"StudentName":"Carol","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ecadd2f684a30fbdfd575") } > db.findListOfIdsDemo.insertOne({"StudentName":"Bob","StudentAge":25}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ecae42f684a30fbdfd576") } > db.findListOfIdsDemo.insertOne({"StudentName":"David","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ecaed2f684a30fbdfd577") } > db.findListOfIdsDemo.insertOne({"StudentName":"John","StudentAge":20}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ecaf82f684a30fbdfd578") } > db.findListOfIdsDemo.insertOne({"StudentName":"Mike","StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c8ecb092f684a30fbdfd579") }
Display all documents from a collection with the help of find() method. The query is as follows −
> db.findListOfIdsDemo.find().pretty();
The following is the output −
{ "_id" : ObjectId("5c8ecadd2f684a30fbdfd575"), "StudentName" : "Carol", "StudentAge" : 21 } { "_id" : ObjectId("5c8ecae42f684a30fbdfd576"), "StudentName" : "Bob", "StudentAge" : 25 } { "_id" : ObjectId("5c8ecaed2f684a30fbdfd577"), "StudentName" : "David", "StudentAge" : 22 } { "_id" : ObjectId("5c8ecaf82f684a30fbdfd578"), "StudentName" : "John", "StudentAge" : 20 } { "_id" : ObjectId("5c8ecb092f684a30fbdfd579"), "StudentName" : "Mike", "StudentAge" : 23 }
Here is the query to find through the list of ids −
> var listOfIds = ['5c8ecae42f684a30fbdfd576', '5c8ecaf82f684a30fbdfd578']; > var documentIds = listOfIds.map(function(myId) { return ObjectId(myId); }); > db.findListOfIdsDemo.find({_id: {$in: documentIds }}).pretty();
The following is the output −
{ "_id" : ObjectId("5c8ecae42f684a30fbdfd576"), "StudentName" : "Bob", "StudentAge" : 25 } { "_id" : ObjectId("5c8ecaf82f684a30fbdfd578"), "StudentName" : "John", "StudentAge" : 20 }
- Related Articles
- How to delete multiple ids in MongoDB?
- Fetch all the ids of MongoDB documents?
- Program to find number of unique people from list of contact mail ids in Python
- How to get a rating average in MongoDB based on duplicate ids?
- MongoDB aggregation of elements with similar ids in different documents?
- MongoDB aggregation to sum product price with similar IDs
- MongoDB query to update all documents matching specific IDs
- Assign ids to each unique value in a Python list
- How to iterate through Java List?
- How to iterate through a list in Python?
- How to loop through collections with a cursor in MongoDB?
- How to search documents through an integer array in MongoDB?
- How to query on list field in MongoDB?
- How to get distinct list of sub-document field values in MongoDB?
- Sum with MongoDB group by multiple columns to calculate total marks with duplicate ids

Advertisements