【问题标题】:Mongodb update and delete operations in a single query单个查询中的MongoDB更新和删除操作
【发布时间】:2018-07-03 21:22:15
【问题描述】:

我有文档,我想用文档的成员之一更新 hostUser,还必须从成员文档中删除记录,并将已删除成员的筹码添加到俱乐部筹码中。

这是示例文档。

{
    "_id" : "1002",
    "hostUser" : "1111111111",
    "clubChips" : 10000,
    "requests" : {},
    "profile" : {
        "clubname" : "AAAAA",
        "image" : "0"
    },
    "tables" : [ 
        "SJCA3S0Wm"
    ],
    "isDeleted" : false,
    "members" : {
        "1111111111" : {
            "chips" : 0,
            "id" : "1111111111"
        },
        "2222222222" : {
            "chips" : 0,
            "id" : "2222222222"
        }
    }
}

这是我尝试过的。

db.getCollection('test').updateMany({"hostUser":"1111111111"},
{"$set":{"hostUser":"2222222222"},"$unset":{"members.1111111111":""}})

【问题讨论】:

  • 如果我也可以检查成员字段中是否存在值将是一个附加组件。

标签: mongodb mongoose mongodb-query


【解决方案1】:

这就是在一次调用 updateMany 中处理 unset 和 set 的方式。您能否澄清一下“检查成员字段中是否存在值”的意思?

db.getCollection('test').updateMany(
    {"hostUser":"1111111111"},
    {
        '$set': {"hostUser":"2222222222"} ,
        '$unset': {"members.1111111111":""}
    }
)

【讨论】:

  • 你给的解决方案我已经试过了。我也想看看有没有memerbs.2222222222
猜你喜欢
  • 2011-01-22
  • 2016-03-15
  • 1970-01-01
  • 2021-09-14
  • 1970-01-01
  • 2020-06-14
  • 2015-02-02
  • 1970-01-01
  • 2018-05-30
相关资源
最近更新 更多