【问题标题】:How to apply the "arrayFilters" on the top-level array?如何在顶级数组上应用“arrayFilters”?
【发布时间】:2018-09-18 06:15:29
【问题描述】:

我的问题类似于Remove Object from Nested Array by Multiple Criteria。主要区别是我想先在顶级数组上应用arrayFilter,然后在匹配的顶级数组元素的嵌套数组上应用另一个arrayFilter

首先,这是我从帖子中复制的文档示例,并进行了一些调整以适合我的情况...

{
    "OtherField1" : ISODate("2016-05-06T05:09:14.589Z"),
    "OtherField2" : "something",
    "Distributions" : [
            {
                    "DeliveryType" : 1,
                    "DeliverySubType" : 2,
                    "DistributionData" : [
                            {
                                    "Key" : "Topic",
                                    "Value" : "Topics",
                                    "Children" : null
                            },
                            {
                                    "Key" : "Message",
                                    "Value" : "test",
                                    "Children" : null
                            }
                    ]
            },
            {
                    "DeliveryType" : 1,
                    "DeliverySubType" : 3,
                    "DistributionData" : [
                            {
                                    "Key" : "Topic",
                                    "Value" : "Topics",
                                    "Children" : ""
                            },
                            {
                                    "Key" : "Message",
                                    "Value" : "test",
                                    "Children" : null
                            }
                    ]
            }
    ]
}

我想要实现的是,首先,通过匹配"DeliveryType" : 1"DeliverySubType" : 3来定位顶层Distributions数组的第二个元素,然后定位其嵌套的DistributionData的第一个元素数组通过匹配"Key": "Topic""Value" : "Topics",最后更新匹配的嵌套数组元素的Children字段。

对于顶级匹配,我尝试了以下形式...

{
  "arrayFilters": [
  { "filter1" : {
      "elemMatch" : {
         "DeliveryType" : 1,
         "DeliverySubType" : 3,
       }
     }
  }
}

并将过滤器引用为Distributions.$[filter1]...,但它不起作用。 或者,

{
  "arrayFilters": [
  { "filter1.Distributions" : {
      "elemMatch" : {
         "DeliveryType" : 1,
         "DeliverySubType" : 3,
       }
     }
  }
}

并将过滤器引用为$[filter1].Distributions....,但它再次失败并出现解析错误。

我能找到的所有示例都是关于在嵌套数组上使用arrayFilters,我自己在这方面取得了一些成功,但无论我多么努力地将相同的技巧应用于顶级数组上的过滤器,他们只是因各种错误而失败。有人可以帮忙吗?谢谢。

【问题讨论】:

    标签: arrays mongodb mongodb-query


    【解决方案1】:

    解决方案有点隐藏,但实际上是documented

    db.collection.update({}, {
        $set: { "Distributions.$[distFilter].DistributionData.$[distDataFilter].Children": "new value" }
        }, {
        "arrayFilters": [
            {
                "distFilter.DeliveryType" : 1,
                "distFilter.DeliverySubType" : 3
            }, {
                "distDataFilter.Key" : "Topic",
                "distDataFilter.Value" : "Topics"
            }
        ]
    })
    

    【讨论】:

    • 非常感谢!进一步的问题,而不是列出关键属性,是否有任何其他语法可以使用像 {"key1":"value1", "key2":"value2"} 这样的对象表示法?谢谢。
    • 我不知道。
    猜你喜欢
    • 1970-01-01
    • 2020-01-15
    • 2020-10-09
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    相关资源
    最近更新 更多