For updating collections and documents we can use any of these methods:
The update() method modifies one or many documents (update parameters)
db.lights.update(
{ room: "Bedroom" },
{ status: "On" }
)
This operation searches the ‘lights’ collection for a document where room
is Bedroom (1st parameter). It then updates the matching documents status
property to On (2nd parameter) and returns a WriteResult object that looks like this:
{ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }
The UpdateOne() method modifies ONE document (update parameters)
db.countries.update(
{ country: "Sweden" },
{ capital: "Stockholm" }
)
This operation searches the ‘countries’ collection for a document where country
is Sweden (1st parameter). It then updates the matching documents property capital
to Stockholm (2nd parameter) and returns a WriteResult object that looks like this:
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }