O'Reilly logo

Hands-On Full Stack Web Development with Aurelia by Erikson Haziz Murrugarra Sifuentes, Diego Jose Arguelles Rojas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Updating documents

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required