【问题标题】:$pull from nested array$从嵌套数组中拉取
【发布时间】:2019-02-05 11:32:24
【问题描述】:

我有以下文档结构。我正在尝试使用 $pull 从组件数组中删除组件。我不知道如何从以下文档中选择“mast”或“commsbox”。

{
    "_id" : ObjectId("23456yujbvfdfg"),
    "d": 1234567,
    "components" : [
        [
            "mast",
            {
                "foo":"bar"
            }
        ],
        [
            "commsbox",
            {
                "BLARN": "bAAA"
            }
        ]
    ]
}

我试过了

db.sites.update({components: {$exists: true}, {$pull: { components.mast: {$exists: true} } }  }).pretty();

但我无法正确选择组件。

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    您需要在嵌套数组中使用$ 运算符来$pull

    db.sites.update(
      { "components": { "$exists": true }},
      { "$pull": { "components.$": "mast" }}
    )
    

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 1970-01-01
      • 1970-01-01
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 2013-10-11
      • 2021-07-17
      相关资源
      最近更新 更多