
- 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 return only a single property “_id” in MongoDB?
Following is the syntax to return only a single property _id in MongoDB
db.yourCollectionName.find({}, {"_id": 1}).pretty();
Let us first create a collection with documents
> db.singlePropertyIdDemo.insertOne({"_id":101,"UserName":"Larry","UserAge":21}); { "acknowledged" : true, "insertedId" : 101 } > db.singlePropertyIdDemo.insertOne({"_id":102,"UserName":"Mike","UserAge":26}); { "acknowledged" : true, "insertedId" : 102 } > db.singlePropertyIdDemo.insertOne({"_id":103,"UserName":"Chris","UserAge":24}); { "acknowledged" : true, "insertedId" : 103 } > db.singlePropertyIdDemo.insertOne({"_id":104,"UserName":"Robert","UserAge":23}); { "acknowledged" : true, "insertedId" : 104 } > db.singlePropertyIdDemo.insertOne({"_id":105,"UserName":"John","UserAge":27}); { "acknowledged" : true, "insertedId" : 105 }
Following is the query to display all documents from a collection with the help of find() method
> db.singlePropertyIdDemo.find().pretty();
This will produce the following output
{ "_id" : 101, "UserName" : "Larry", "UserAge" : 21 } { "_id" : 102, "UserName" : "Mike", "UserAge" : 26 } { "_id" : 103, "UserName" : "Chris", "UserAge" : 24 } { "_id" : 104, "UserName" : "Robert", "UserAge" : 23 } { "_id" : 105, "UserName" : "John", "UserAge" : 27 }
Following is the query to return only a single property _id
> db.singlePropertyIdDemo.find({}, {"_id": 1}).pretty();
This will produce the following output
{ "_id" : 101 } { "_id" : 102 } { "_id" : 103 } { "_id" : 104 } { "_id" : 105 }
- Related Articles
- How to return only value of a field in MongoDB?
- Remove only a single document in MongoDB
- Update only a single document in MongoDB
- Decrement only a single value in MongoDB?
- How to update only one property in MongoDB?
- How to find only a single document satisfying the criteria in MongoDB?
- MongoDB, finding all documents where property id equals to record id?
- Increment only a single value in MongoDB document?
- How to return only unique values (no duplicates) in MongoDB?
- Selecting only a single field from MongoDB?
- MongoDB query to return only embedded document?
- Querying only the field name and display only the id in MongoDB?
- Retrieve only a single document specifying a criteria in MongoDB?
- MongoDB findById returning a list of documents instead of a single result? How to get only a single document?
- Return only a single row from duplicate rows with MySQL

Advertisements