mongodbHow to use MongoDB pull?
MongoDB's $pull
operator is used to remove an item from an array. It takes two arguments: the first is the name of the field containing the array, and the second is the value to be removed.
Example
db.collection.update(
{ _id: 1 },
{ $pull: { arrayField: "valueToRemove" } }
)
Output example
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Code explanation
db.collection.update
: This is the MongoDB command used to update a document in a collection.{ _id: 1 }
: This is the query used to select the document to update.{ $pull: { arrayField: "valueToRemove" } }
: This is the update operator used to remove an item from an array. The first argument is the name of the field containing the array, and the second is the value to be removed.
Helpful links
More of Mongodb
- How to empty an array in 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 update an array element in MongoDB?
- How to update many documents in MongoDB?
- How to create a many to many relation in MongoDB?
- How to join two collections in MongoDB?
- How to use triggers in MongoDB?
- How to use hint in MongoDB?
See more codes...