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://www.11000011.com/?id=33

« 上一篇 下一篇 »
«    2026年7月    »
12345
6789101112
13141516171819
20212223242526
2728293031

控制面板

您好,欢迎到访网站!
  查看权限
«    2026年7月    »
12345
6789101112
13141516171819
20212223242526
2728293031

控制面板

您好,欢迎到访网站!
  查看权限
您是本站第2340名访客 今日有0篇新文章