MongoDB更新数组中的错误子文档

用户名

我的游戏家族收藏看起来像这样

{
    "_id": ObjectId('54cc3ee7894ae60c1c9d6c74'),
    "game_ref_id": "REF123",
    ..
    "yearwise_details": [
    {
        "year": 1,
        ...
        "other_details": [
            {
                "type": "cash",
                "openingstock": 988
                ..
            },
            {
                "type": "FLU",
                "openingstock": 555
                ..
            },
            ..other items
        ]
    },
    {
        "year": 2,
        ...

        "other_details": [
            {
                "type": "cash",
                "openingstock": 3000,
                ....
            },
            ...
            {
                "type": "ghee",
                "openingstock": 3000,
                ...
            },
            ..
        ]
    }
]
}

我的更新查询

db.gamefamilies.update({“ game_ref_id”:“ REF123”,“ teamname”:“ manisha”,“ yearwise_details.year”:2,“ yearwise_details.other_details.type”:“ ghee”},{“ $ set”: {“ yearwise_details.0.other_details。$。openingstock”:555}});

正确拾取文档。我希望更新第2年的项目类型=“ ghee”,但会更新第1年的第二项(类型FLU)。我究竟做错了什么 ?

任何帮助将不胜感激。

问候Manisha

无插座

不幸的是,尚不支持嵌套的$位置运算符更新。

因此,您可以使用以下方式对更新进行硬编码:

db.gamefamilies.update({"game_ref_id": "REF123",
                        "teamname": "manisha",
                        "yearwise_details.year": 2,
                        "yearwise_details.other_details.type": "ghee"},
                       {"$set":
                         {"yearwise_details.1.other_details.$.openingstock": 555}});

但是请注意,yearwise_details.1.other_details您要对数组的第二个值进行硬编码(它是0索引的,因此,1它引用了第二个元素)。我假设您找到了问题中的命令,因为它适用于数组的第一个元素。但是它只会在第一个元素上起作用,而上面的命令只会在第二个元素上起作用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章