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 check the version of MongoDB?
- How to use watch in MongoDB?
- How to update one document in MongoDB?
- How to update many documents in MongoDB?
- How to use MongoDB queue?
- How to create a many to many relation in MongoDB?
- How to insert new document into MongoDB?
- How to use hint in MongoDB?
- How to work with time series data in MongoDB?
- How to use unwind in MongoDB?
See more codes...