1

I am very new to mongoDB.

i have this collection here:

{ name: Peter salary: 3000 }, { name: Marry salary: 2000 }, { name: Sally salary: 1000 }

What should I do if I want to increase all salaries by 500?

I tried db.bounties.update({},{$inc: { salary: 500}}) but only the first one got updated

  • 1
    Does this answer your question? [MongoDB: How to update multiple documents with a single command?](https://stackoverflow.com/questions/1740023/mongodb-how-to-update-multiple-documents-with-a-single-command) – MartenCatcher May 24 '20 at 06:07

1 Answers1

3

there're two ways

1- use updateMany it means Updates all documents that match the specified filter for a collection

db.bounties.updateMany({},{$inc: { salary: 500}})

2- use update with multi parameter

db.bounties.update({},{$inc: { salary: 500}},{multi:true})

Hossam
  • 367
  • 3
  • 9