
- 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 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 Articles
- MongoDB query to update all documents matching specific IDs
- MongoDB compound conditions to fetch documents?
- Fetch specific multiple documents in MongoDB
- 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?
- Match ID and fetch documents with $eq in MongoDB in case of array?
- Get the size of all the documents in a MongoDB query?
- How to update all documents in MongoDB?
- Group all documents with common fields in MongoDB?
- MongoDB query for counting the distinct values across all documents?
- MongoDB query to implement nor query to fetch documents except a specific document
- MongoDB inverse of query to return all items except specific documents?
- How to delete all the documents from a collection in MongoDB?

Advertisements