updateMany and $inc

updateMany() changes every document that matches the filter. This example also uses $inc, which adds a number to a field:

db.movies.updateMany(
  { director: "Damien Chazelle" },
  { $inc: { votes: 50 } }
)
db.movies.find(
  { director: "Damien Chazelle" },
  { _id: 0, title: 1, votes: 1 }
)

Output:

{
  acknowledged: true,
  insertedId: null,
  matchedCount: 3,
  modifiedCount: 3,
  upsertedCount: 0
}
[
  { title: 'Whiplash', votes: 1550 },
  { title: 'La La Land', votes: 1750 },
  { title: 'First Man', votes: 850 }
]

Three movies matched, and all three were changed. Each one got 50 more votes. To subtract, give $inc a negative number, like { $inc: { votes: -50 } }.

You could also read each value, add 50 yourself, and $set the new value. $inc is better. It does the change in one step on the server, so it stays correct even when two programs update the same document at the same time.

There are more update operators. $mul multiplies a number, $unset removes a field, and $rename gives a field a new name. You can use several operators in one update, like { $inc: { votes: 1 }, $set: { checked: true } }.

Be careful with the filter. updateMany({}, ...) matches every document, so it changes the whole collection. If you are not sure, run countDocuments with the same filter first and check the number.

In the exercise, give every Christopher Nolan movie 100 more votes.