June 2018
Intermediate to advanced
348 pages
8h 19m
English
To update a document, we use the updateOne or updateMany methods. For example, let's update the ranking property of the Peru team. Execute the following code:
> db.teams.updateOne({"code": "PER"}, {$set: {"ranking": 1}}){ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
The syntax looks a little weird, but it is not. The first question you might have is why do we need $set? We need $set to specify the fields that we are interested to update. Otherwise, you will replace the document. Consider this example:
First, let's list our teams' collection to see the first update reflected:
> db.teams.find({"code": "PER"}){ "_id" : ObjectId("5a5cf1419afc8af268b9bb21"), "code" : "PER", "name" : "Peru", "ranking" : 1, ...Read now
Unlock full access