How can I delete an item from an Object in MongoDB?

To delete an item from an object in MongoDB, use the $unset operator. This operator removes specific fields from documents, including nested object fields using dot notation.

Syntax

db.collection.update(
    { query },
    { $unset: { "fieldName": 1 } }
);

// For nested objects
db.collection.update(
    { query },
    { $unset: { "object.field": 1 } }
);

Sample Data

db.demo467.insertMany([
    {
        _id: 101,
        "Information": { "Name": "Chris" }
    },
    {
        _id: 102,
        "Information": { "Name": "David" }
    }
]);
{ "acknowledged": true, "insertedIds": { "0": 101, "1": 102 } }
db.demo467.find();
{ "_id": 101, "Information": { "Name": "Chris" } }
{ "_id": 102, "Information": { "Name": "David" } }

Example: Delete Object Field

Remove the "Name" field from the Information object for document with _id 102 ?

db.demo467.updateOne(
    { _id: 102 },
    { $unset: { "Information.Name": 1 } }
);
{ "acknowledged": true, "matchedCount": 1, "modifiedCount": 1 }

Verify Result

db.demo467.find();
{ "_id": 101, "Information": { "Name": "Chris" } }
{ "_id": 102, "Information": { } }

Key Points

  • Use dot notation to target nested object fields: "object.field"
  • The value after the field name can be any value (commonly 1 or "")
  • $unset completely removes the field, leaving an empty object if no other fields exist

Conclusion

The $unset operator effectively removes specific fields from MongoDB objects using dot notation. This is useful for cleaning up document structure or removing unwanted nested properties.

Updated on: 2026-03-15T03:05:00+05:30

772 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements