Can we remove _id from MongoDB query result?

To remove _id from MongoDB query result, you need to set 0 for the _id field in the projection parameter. This excludes the _id field from the returned documents.

Syntax

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

Sample Data

Let us create a collection with sample documents ?

db.removeIdDemo.insertMany([
    {"UserName": "John", "UserAge": 23},
    {"UserName": "Mike", "UserAge": 27},
    {"UserName": "Sam", "UserAge": 34},
    {"UserName": "Carol", "UserAge": 29}
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5c9bb4042d66697741252440"),
        ObjectId("5c9bb40c2d66697741741252441"),
        ObjectId("5c9bb4162d66697741252442"),
        ObjectId("5c9bb4222d66697741252443")
    ]
}

Example 1: Default Query (With _id)

First, let us display all documents with the default behavior ?

db.removeIdDemo.find();
{
    "_id": ObjectId("5c9bb4042d66697741252440"),
    "UserName": "John",
    "UserAge": 23
}
{
    "_id": ObjectId("5c9bb40c2d66697741252441"),
    "UserName": "Mike",
    "UserAge": 27
}
{
    "_id": ObjectId("5c9bb4162d66697741252442"),
    "UserName": "Sam",
    "UserAge": 34
}
{
    "_id": ObjectId("5c9bb4222d66697741252443"),
    "UserName": "Carol",
    "UserAge": 29
}

Example 2: Remove _id from Result

Now, let us exclude the _id field from the query result ?

db.removeIdDemo.find({}, {_id: 0});
{"UserName": "John", "UserAge": 23}
{"UserName": "Mike", "UserAge": 27}
{"UserName": "Sam", "UserAge": 34}
{"UserName": "Carol", "UserAge": 29}

Key Points

  • By default, MongoDB always includes the _id field in query results.
  • Use {_id: 0} in the projection parameter to exclude it.
  • This works with any query condition, not just empty queries.

Conclusion

Setting {_id: 0} in the projection parameter effectively removes the _id field from MongoDB query results. This is useful when you only need the actual data fields without the unique identifier.

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

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements