arrays - 更新mongodb数组中的多个元素
问题描述
我在 mongodb 的股票集合中有如下文件。
{ _id: 'xRMuRBhqRLgASyQyW',
History:
[ { orderId: '12032017001877',
status: 'COMPLETED',
},
{ orderId: '22122017002450',
status: 'PROCESSED',
},
{ orderId: '22122018002450',
status: 'DEPOSIT',
}
]
}
如果状态不是,我想遍历stocks
集合中的所有文档并添加一个字段。flag: true
'PROCESSED'
解决方案
您需要使用 all $[] 位置运算符来更新数组中的每个元素
db.collection.update(
{ "History": { "$elemMatch": { "status": { "$ne": "PROCESSED" } } } },
{ "$set": { "History.$[].flag": false } },
{ "multi": true }
)
推荐阅读
- tfs - TFS 2017 将项目添加到看板上的“新项目”
- oracle - oracle DB服务器上客户端进程的内存消耗
- node.js - 启动chrome headless后无法执行命令
- c# - 从另一个泛型方法调用 MaybeNull 泛型方法
- javascript - 使用 Javascript 和 Webpack 加载图像数组?
- sql - REGEXP_LIKE、oracle、sql、查询、理解
- arm - 树莓派模型 3b+ 活动 LED 沿 gpio 4 闪烁
- mysql - 如何在没有行重复的情况下获得结果
- javascript - 正则表达式:匹配模式,除了前面的模式
- wpf - WPF按钮未触发单击事件