mongodbHow to empty an array in MongoDB?
To empty an array in MongoDB, you can use the $unset
operator. This operator will remove the specified field from the document.
For example, to empty the tags
array in the document { _id: 1, tags: ["a", "b", "c"] }
, you can use the following command:
db.collection.update({ _id: 1 }, { $unset: { tags: "" } })
The command above will result in the following document:
{ _id: 1 }
The command consists of the following parts:
db.collection.update
: This is the command used to update a document in MongoDB.{ _id: 1 }
: This is the filter used to identify the document to be updated.{ $unset: { tags: "" } }
: This is the update operator used to remove thetags
field from the document.
Helpful links
More of Mongodb
- How to use watch in MongoDB?
- How to check the version of MongoDB?
- How to use triggers in MongoDB?
- How to use unwind in MongoDB?
- How to list MongoDB users?
- How to sort MongoDB query results?
- How to convert MongoDB ObjectId to string?
- How to rename a field in MongoDB?
- How to perform a health check for MongoDB?
See more codes...