mongodbHow to remove a field from MongoDB?
To remove a field from MongoDB, use the $unset
operator. This operator removes the specified field from the document.
Example
db.collection.update(
{ _id: 1 },
{ $unset: { field: "" } }
)
The output of the above example will be:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Code explanation
db.collection.update
: This is the command used to update a document in MongoDB.{ _id: 1 }
: This is the filter used to specify the document to update.{ $unset: { field: "" } }
: This is the update operator used to remove the specified field from the document.
Helpful links
More of Mongodb
- How to rename a field in MongoDB?
- How to update many documents in MongoDB?
- What is MongoDB default port?
- How to specify a password for MongoDB Docker?
- How to list MongoDB users?
- How to use regex in MongoDB?
- How to check the version of MongoDB?
- How to use MongoDB queue?
- How to query with "not equal" condition in MongoDB?
- How to create a many to many relation in MongoDB?
See more codes...