0

I have a database named MongoLab1 and a collections named employees.

I want to update all documents by increasing the salary by 5000. I'm going to use $inc

This is my code in creating the database:

db.employees.insertMany( [ {"Name": "Stephen Jackson", "Salary": 16099.55, "Position": "President", 
"Rank": 1, "ReportingTo": null} ] )

db.employees.insertMany( [ {"Name": "Michael Harrison", "Salary": 14567.12, "Position": "Vice- 
President", "Rank": 2, "ReportingTo": "[President]"} ] )

db.employees.insertMany( [ {"Name": "Rex Andres", "Salary": 13891.22, "Position": "Secretary", 
"Rank": 3, "ReportingTo": "[Vice-President]"} ] )

db.employees.insertMany( [ {"Name": "Sam Johnson", "Salary": 13000, "Position": "Treasurer", "Rank": 
4, "ReportingTo": "[Secretary, Vice-President]"} ] )

I tried this code:

db.employees.updateMany($inc: {"Salary": 5000})

However, this don't work and causes an error.

Zak
  • 57
  • 1
  • 7
  • 1
    You need to specify both a query (to target the documents to update), and the update. The query selector can be empty if you want to target all, but it has to be specified. – Thilo Jan 07 '21 at 11:26
  • 1
    From the marked duplicate, to update all documents follow `db.employees.updateMany({}, { "$inc": { "Salary": 5000 } })` – chridam Jan 07 '21 at 11:55

0 Answers0