我有一个Mongo文档,其中包含一系列元素.
我想重置.handled数组中所有对象的属性.profile= XX.
该文件采用以下形式:
{
"_id": ObjectId("4d2d8deff4e6c1d71fc29a07"),
"user_id": "714638ba-2e08-2168-2b99-00002f3d43c0",
"events": [{
"handled": 1,
"profile": 10,
"data": "....."
} {
"handled": 1,
"profile": 10,
"data": "....."
} {
"handled": 1,
"profile": 20,
"data": "....."
}
...
]
}
Run Code Online (Sandbox Code Playgroud)
所以,我尝试了以下内容:
.update({"events.profile":10},{$set:{"events.$.handled":0}},false,true)
Run Code Online (Sandbox Code Playgroud)
但是,它仅更新每个文档中第一个匹配的数组元素.(这是$的定义行为- 位置运算符.)
如何更新所有匹配的数组元素?
我\xe2\x80\x99一直在尝试更新我的mongoDB中的数据。\n我想使用新的productName字段更新所有产品。
\n我的数据看起来像:
\n{\n "id": "12345",\n "products": [{\n "id": 0\n "productCode": "test",\n "status": "PENDING",\n },\n {\n "id": 1\n "productCode": "test",\n "status": "COMPLETE",\n }],\n}\nRun Code Online (Sandbox Code Playgroud)\n当我尝试以下操作时。我收到此错误位置运算符未从查询中找到所需的匹配项。
\ndb.customers.updateMany(\n { id: "12345" },\n { $set: {\n "products.$.productName": "Name here" } \n }\n)\nRun Code Online (Sandbox Code Playgroud)\n如果我执行 account.0.productName 那么它 \xe2\x80\x99s 很好并更新。I\xe2\x80\x99m 不确定为什么 $ 对我不起作用
\ndb.customers.updateMany(\n { id: "12345" },\n { $set: {\n "products.0.productName": "Name here" } \n }\n)\nRun Code Online (Sandbox Code Playgroud)\n