// plain
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.
db.collection.update(
{ _id: 1 },
{ $pull: { arrayField: "valueToRemove" } }
)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
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.
onelinerhub: How to use MongoDB pull?