Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何更新Mongodb中的多个grand child元素_Arrays_Mongodb_Updates - Fatal编程技术网

Arrays 如何更新Mongodb中的多个grand child元素

Arrays 如何更新Mongodb中的多个grand child元素,arrays,mongodb,updates,Arrays,Mongodb,Updates,我正在尝试更新MongoDB孙子文档以更新大元素值。例如,我需要将所有“ware”值更新为“LUX”,其中“ware”是“LAX” 换句话说,我需要改变 “陶器”:“LAX”,-->“陶器”:“勒克斯”, 但是,“ware”:“NYC”不应更改 下面是示例数据 [ { "_id": ObjectId("5db72c636309f84479ec0c7b"), "productCode": "aaaa", "brand": "Nike", "image": "some.jpg", "sizes":

我正在尝试更新MongoDB孙子文档以更新大元素值。例如,我需要将所有“ware”值更新为“LUX”,其中“ware”是“LAX”

换句话说,我需要改变 “陶器”:“LAX”,-->“陶器”:“勒克斯”, 但是,“ware”:“NYC”不应更改

下面是示例数据

[
{
"_id": ObjectId("5db72c636309f84479ec0c7b"),
"productCode": "aaaa",
"brand": "Nike",
"image": "some.jpg",
"sizes": [
    {
    "_id": ObjectId("5db72c636309f84479ec0c7e"),
    "size": "41",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c80"),
        "ware": "LAX",
        "amount": 100
        },
        {
        "_id": ObjectId("5db72c636309f84479ec0c7f"),
        "ware": "NYC",
        "amount": 7
        }
    ]
    },
    {
    "_id": ObjectId("5db72c636309f84479ec0c7c"),
    "size": "42",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c7d"),
        "ware": "LAX",
        "amount": 16
        }
    ]
    }
]
},
{
"_id": ObjectId("5db72c636309f84479ec0c7X"),
"productCode": "aaaa",
"brand": "Nike",
"image": "some.jpg",
"sizes": [
    {
    "_id": ObjectId("5db72c636309f84479ec0c7D"),
    "size": "41",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c8G"),
        "ware": "LAX",
        "amount": 100
        },
        {
        "_id": ObjectId("5db72c636309f84479ec0c7R"),
        "ware": "NYC",
        "amount": 7
        }
      ]
    },
    {
    "_id": ObjectId("5db72c636309f84479ec0c7q"),
    "size": "42",
    "wares": [
        {
        "_id": ObjectId("5db72c636309f84479ec0c7n"),
        "ware": "LAX",
        "amount": 16
        }
      ]
    }
  ]
}
]
我试过这个

db.getCollection('products').findAndModify({
query: { sizes: { $elemMatch: { wares:  {$elemMatch: { ware: "LAX" }}}}},
update: { $set: { "sizes.wares.$.ware": 'LUX' } }
})
试试这个:

db.getCollection('products').updateMany(
   {},
   { $set: { "sizes.$[].wares.$[item].ware": 'LUX' } },
   { arrayFilters: [{ "item.ware": "LAX" }] }
)

这不起作用:(收到此消息:WriteError:在路径大小中找不到标识符'item'的数组筛选器。$[].wares.$[item].ware:WriteError({“索引”:0,“代码”:2,“errmsg):“在路径大小中找不到标识符'item'的数组筛选器。$[].wares.$[item].ware',“op:{“q:{},”u:{“$set:{”大小。$[].wares.$[item].ware:“LUX”},“multi”:true,“upsert”:false}})命令正在运行(版本4.2),请检查语法和/或提供有效的示例文档。您的
ObjectId(…)例如,
是无效的。3.6.0版有没有办法做到这一点根据文档
updateMany
是在3.2中引入的,
arrayFilters
是在3.6中引入的-所以它应该在jmongoshel上实际工作,但在Robomongo:)所以问题解决了,谢谢!