mongodb批量修改数组下元素某个字段值


mongodb批量修改数组下元素某个字段

例如下面的数据,如果想把alias为ooo的des都改成haha

{
    "name": "test",
    "items": [
        {
            "id": 1,
            "content": {
                "cid": "123", 
                "des": "hello",
                "alias": "ooo"
            }
        },
        {
            "id": 2,
            "content": {
                "cid": "124", 
                "des": "world",
                "alias": "ooo"
            }
        }
    ]
}

查找操作,通过elemMatch处理,如下

db.test.find({"items":{$elemMatch:{"content.alias": "ooo"}}})

修改操作,通过$符来操作

db.test.find({"items":{$elemMatch:{"content.alias": "ooo"}}},{$set:{"items.$.content.des":"haha"}}, {multi: true})

一不小心加错了字段可以用删除

db.test.update({"items":{$elemMatch:{"content.alias": "ooo"}}},{$unset:{"xxxx":""}},false,true)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://11000011.com/?id=33

« 上一篇 下一篇 »

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

主机推荐

  • 搬瓦工