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 work with time series data in MongoDB?
- How to use watch in MongoDB?
- How to check the version of MongoDB?
- How to perform a health check for MongoDB?
- How to create a many to many relation in MongoDB?
- How to use triggers in MongoDB?
- How to aggregate in MongoDB?
- How to list all indexes in MongoDB?
- How to update an array element in MongoDB?
- How to list MongoDB users?
See more codes...