Updating Documents
Last updated
Was this helpful?
Last updated
Was this helpful?
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.
Official documentation: β
$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
$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.
Will make the same modification to all documents that match the filter.
$unset operator will remove all the values that match the filter.
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.
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.