mongodb - 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.
解决方案
推荐阅读
- javascript - 如何使用 mongoose 和 nodeJS 在 MongoDB 集合中复制或克隆文档
- electron - 如何从渲染器进程访问命令行参数现在远程已弃用
- flutter - 如何摆脱列内文本的像素溢出?
- ajax - 网络核心 3 中的 Ajax CSRF 验证失败,错误 400
- reactjs - React - 使用 useState 钩子时使用获取请求的无限循环
- python - InfluxDB 2 入门样例没有向桶中插入任何数据,没有错误,没有异常
- python - 如何连接张量并将其保存到文本文件中?
- java - 在反应式编程中,doOnNext 调用的顺序是否得到保证?
- r - 自定义函数返回不正确的值 - 示例(冲突的向量名称)
- sql - 更新子查询中每一行的值