首页 > 解决方案 > In MongoDB, how to update all the documents by increasing a value

问题描述

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.

标签: mongodbincrement

解决方案


推荐阅读