Removing _id element from PyMongo results?

To remove the _id element from MongoDB query results, use projection with {'_id': false} in the find() method. This excludes the _id field from the returned documents.

Syntax

db.collection.find({}, {'_id': false})

Sample Data

db.removingidElementDemo.insertMany([
    {"UserName": "John", "UserAge": 21},
    {"UserName": "Carol", "UserAge": 24},
    {"UserName": "David", "UserAge": 22},
    {"UserName": "Mike", "UserAge": 26},
    {"UserName": "Chris", "UserAge": 20}
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5c9153fd4afe5c1d2279d6ad"),
        ObjectId("5c9154084afe5c1d2279d6ae"),
        ObjectId("5c9154154afe5c1d2279d6af"),
        ObjectId("5c9154204afe5c1d2279d6b0"),
        ObjectId("5c91542c4afe5c1d2279d6b1")
    ]
}

Example 1: Display All Documents (With _id)

db.removingidElementDemo.find().pretty();
{
    "_id": ObjectId("5c9153fd4afe5c1d2279d6ad"),
    "UserName": "John",
    "UserAge": 21
}
{
    "_id": ObjectId("5c9154084afe5c1d2279d6ae"),
    "UserName": "Carol",
    "UserAge": 24
}
{
    "_id": ObjectId("5c9154154afe5c1d2279d6af"),
    "UserName": "David",
    "UserAge": 22
}

Example 2: Remove _id from Results

db.removingidElementDemo.find({}, {'_id': false}).pretty();
{ "UserName": "John", "UserAge": 21 }
{ "UserName": "Carol", "UserAge": 24 }
{ "UserName": "David", "UserAge": 22 }
{ "UserName": "Mike", "UserAge": 26 }
{ "UserName": "Chris", "UserAge": 20 }

Key Points

  • The _id field is included by default in all MongoDB queries
  • Use {'_id': false} in the projection parameter to exclude it
  • This approach works with any query conditions, not just empty queries

Conclusion

Use projection with {'_id': false} to exclude the _id field from MongoDB query results. This provides cleaner output when the _id field is not needed.

Updated on: 2026-03-15T00:18:57+05:30

693 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements