arrays - Mongodb位置更新所有$ []似乎不起作用
问题描述
我有一个这样的事件集合:
{title: "Some Title", description: "Some Description", products: [{_id: "owkaowakdowakd", quantity: 15}]}
我想在 products 数组的所有元素中添加一个带有空数组的“保留”字段。要得到这个结果:
{title: "Some Title", description: "Some Description", products: [{_id: "owkaowakdowakd", quantity: 15, reserved: [] }]}
查看文档,您可以使用 $[] 运算符对数组的所有元素进行更改。https://docs.mongodb.com/manual/reference/operator/update/positional-all/#positional-update-all
db.events.updateMany( {"products.reserved": {$exists: false}}, {$set: {"products.$[].reserved": []}})
但这会给我一个错误:
2019-12-17T16:03:43.399+0000 E QUERY [js] WriteError({
"index" : 0,
"code" : 28,
"errmsg" : "Cannot create field 'reserved' in element {0: null}",
"op" : {
"q" : {
"products.reserved" : {
"$exists" : false
}
},
"u" : {
"$set" : {
"products.$[].reserved" : [ ]
}
},
"multi" : true,
"upsert" : false
}
}) :
WriteError({
"index" : 0,
"code" : 28,
"errmsg" : "Cannot create field 'reserved' in element {0: null}",
"op" : {
"q" : {
"products.reserved" : {
"$exists" : false
}
},
"u" : {
"$set" : {
"products.$[].reserved" : [ ]
}
},
"multi" : true,
"upsert" : false
}
})
我还尝试修改每个元素的数量,因为这已经存在,但它返回相同的错误!“无法创建字段‘数量’……”
解决方案
希望这会有所帮助
推荐阅读
- android - 如何使用可能的待处理意图从服务导航到导航片段?
- angular - 如何在 HTML 字符串中使用 routerLink?
- css - 如何更改primefaces日历中的按钮名称
- html - 我如何向客户提供包含他们的打印输入数据的表格?
- node.js - 你如何用 Node.js 解释 Http 错误消息的奇怪行为
- python - 我应该如何使用 python 和 BS4 从网页中正确提取和解析主题数据?
- javascript - Angular如何在表单提交上获取按钮元素
- python - 列表中的列表组合
- ios - 从 UIDocument 应用程序中的图像添加缩略图
- dart - 我试图将数据添加到 Firestore,但“Firestore 组件不存在”。终端给出这个错误信息