【发布时间】: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