filmov
tv
MongoDB Update: 5 Things You (2 Min) Should Know | updateOne vs. updateMany vs. findOneAndUpdate

Показать описание
In this tutorial, you'll learn how to update document in MongoDB using updateOne, updateMany and findOneAndUpdate methods.
—
—
Video Transcript:
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn five ways to update documents in the MongoDB database. Number one, use the update 1 method to update a single document that matches the given filter.
Here I'm updating the document with the id ending in 865 and setting a new APN and a new subdivision. Number two, if multiple documents are returned then the update 1 method will only update the first document. In this example, I have two records that have the subdivision of willow acres but when I run update 1 only one of those documents will be set to subdivision of new acres.
I'm going to revert this back before we move on to the next example. Number three, use the update menu method to update all the documents that match the given filter. Here I'm updating all the documents which the subdivision of willow acres to new acres.
Number four, leverage the find one and update method to return the original document before updating it. Here I'm updating the document with the id ending in 865 with a new APN and a new subdivision of Rancho Rio 3. Last but not the least number five.
When using the find one and update method you can set the return new document option to true if you want to return the new document instead of the original. Here I'm setting a new APN and a new subdivision for the document with an id ending in 865. When I execute this command you'll notice that the new document was returned.
There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next.
—
// 1. updateOne updates a single document that matches the given filter
{$set:{"apn":23345, "subdivision":"Rancho Rio2"}})
// 2. multiple documents returned
{$set:{"subdivision":"Willow Acres"}})
// 3. updateMany updates all the documents that matches the given filter
{$set:{"subdivision":"New Acres"}})
// 4. findOneAndUpdate returns the original document before updating one
{$set:{"apn":11111, "subdivision":"Rancho Rio3"}})
//
// 5. set returnNewDocument: true if you want to return the new document instead of original
{$set:{"apn":23346, "subdivision":"Rancho Rio"}},
{returnNewDocument: true})
—
—
Video Transcript:
—
Hi guys, this is Abhi from Gokcedb. In this video, you're going to learn five ways to update documents in the MongoDB database. Number one, use the update 1 method to update a single document that matches the given filter.
Here I'm updating the document with the id ending in 865 and setting a new APN and a new subdivision. Number two, if multiple documents are returned then the update 1 method will only update the first document. In this example, I have two records that have the subdivision of willow acres but when I run update 1 only one of those documents will be set to subdivision of new acres.
I'm going to revert this back before we move on to the next example. Number three, use the update menu method to update all the documents that match the given filter. Here I'm updating all the documents which the subdivision of willow acres to new acres.
Number four, leverage the find one and update method to return the original document before updating it. Here I'm updating the document with the id ending in 865 with a new APN and a new subdivision of Rancho Rio 3. Last but not the least number five.
When using the find one and update method you can set the return new document option to true if you want to return the new document instead of the original. Here I'm setting a new APN and a new subdivision for the document with an id ending in 865. When I execute this command you'll notice that the new document was returned.
There you have it. Make sure you like, subscribe, and turn on the notification bell. Until next.
—
// 1. updateOne updates a single document that matches the given filter
{$set:{"apn":23345, "subdivision":"Rancho Rio2"}})
// 2. multiple documents returned
{$set:{"subdivision":"Willow Acres"}})
// 3. updateMany updates all the documents that matches the given filter
{$set:{"subdivision":"New Acres"}})
// 4. findOneAndUpdate returns the original document before updating one
{$set:{"apn":11111, "subdivision":"Rancho Rio3"}})
//
// 5. set returnNewDocument: true if you want to return the new document instead of original
{$set:{"apn":23346, "subdivision":"Rancho Rio"}},
{returnNewDocument: true})