How to return only a single property "_id" in MongoDB?

To return only the _id property in MongoDB, use the projection parameter in the find() method by setting {"_id": 1}. This filters the output to show only the _id field from all matching documents.

Syntax

db.collectionName.find({}, {"_id": 1});

Sample Data

Let us create a collection with sample documents :

db.singlePropertyIdDemo.insertMany([
    {"_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}
]);
{
    "acknowledged": true,
    "insertedIds": {
        "0": 101,
        "1": 102,
        "2": 103,
        "3": 104,
        "4": 105
    }
}

Display all documents to verify the data :

db.singlePropertyIdDemo.find();
{ "_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 }

Example

Return only the _id property from all documents :

db.singlePropertyIdDemo.find({}, {"_id": 1});
{ "_id": 101 }
{ "_id": 102 }
{ "_id": 103 }
{ "_id": 104 }
{ "_id": 105 }

Key Points

  • The {"_id": 1} projection parameter includes only the _id field in the output.
  • MongoDB includes the _id field by default, so {"_id": 1} explicitly shows only this field.
  • Use {"_id": 0} to exclude the _id field when projecting other fields.

Conclusion

Use the projection parameter {"_id": 1} in the find() method to return only the _id property. This is useful when you need to retrieve document identifiers without loading other field data.

Updated on: 2026-03-15T00:29:34+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements