Updating Documents
π updateOne()
updateOne will only update the first one that matches the filter
First, specify the filter.
Second, specify the βhowβ.
$set operator uses a document as an argument and uses all the key values from the document to update the document that matches the filter.
π€ Update Operators
Official documentation: https://docs.mongodb.com/manual/reference/operator/update/#id1β
For scalar values
$set, Sets the value of a field in a document.
$unset, Removes the specified field from a document.
$inc, increment a value by the specified value to a field
For array values
$pop, $pull, $pullAll,
$push creates an array if this does not already exist, each is a modifier to use every element as an independent element for the array.
π updateMany()
Will make the same modification to all documents that match the filter.
$unset operator will remove all the values that match the filter.
π€ Upserts
Update documents that match the filter if there are none insert the update document as a new document in the collection.
Here you are using a variable called detail that has a documentβs detail.
The upsert word is a keyword, executed when trying to update a document and you do not find it, you insert one instead.
π replaceOne()
Variables: filter, doc
At mongodb shell (a javascript interpreter :o) define filter variable.
The doc variable will get one document that matches the filter conditions. Now modify the doc variable, to add a genre and a poster field.
π‘ Note: this method will only replace the first document it finds.
Last updated
Was this helpful?