

- 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
Fetch all the ids of MongoDB documents?
To fetch all the ids, simply use find() in MongoDB. Let us create a collection with documents −
> db.demo169.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e36975e9e4f06af551997d7") } > db.demo169.insertOne({"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3697629e4f06af551997d8") } > db.demo169.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3697679e4f06af551997d9") }
Display all documents from a collection with the help of find() method −
> db.demo169.find();
This will produce the following output −
{ "_id" : ObjectId("5e36975e9e4f06af551997d7"), "StudentName" : "Chris" } { "_id" : ObjectId("5e3697629e4f06af551997d8"), "StudentName" : "Bob" } { "_id" : ObjectId("5e3697679e4f06af551997d9"), "StudentName" : "David" }
Following is the query to get document ids of all the MongoDB documents in a collection −
> var iterator=db.demo169.find({},{"StudentName":0});
Display all documents from a collection with the help of find() method −
> iterator;
This will produce the following output −
{ "_id" : ObjectId("5e36975e9e4f06af551997d7") } { "_id" : ObjectId("5e3697629e4f06af551997d8") } { "_id" : ObjectId("5e3697679e4f06af551997d9") }
- Related Questions & Answers
- MongoDB query to update all documents matching specific IDs
- Fetch specific multiple documents in MongoDB
- MongoDB compound conditions to fetch documents?
- MongoDB aggregation of elements with similar ids in different documents?
- MongoDB to fetch documents with $or Operator
- Fetch multiple documents in MongoDB query with OR condition?
- MongoDB aggregation to fetch documents with specific field value?
- Get the size of all the documents in a MongoDB query?
- How to update all documents in MongoDB?
- Match ID and fetch documents with $eq in MongoDB in case of array?
- MongoDB query for counting the distinct values across all documents?
- Group all documents with common fields in MongoDB?
- Get all the IDs of the Time Zone in Java
- MongoDB inverse of query to return all items except specific documents?
- How to delete all the documents from a collection in MongoDB?
Advertisements