[英]MongoDB Aggregation - match documents with array of objects, by another array of objects filter
[英]mongodb - match if a group of values from an array is in another given array [with Aggregation]
自 3 小时以来,我一直在寻找解决方案,但我还没有得到它。
我有以下收藏:
{text: 'random text 1', indexes:[1,2] },
{text: 'random text 2', indexes:[1,3] },
{text: 'random text 3', indexes:[2,4] },
我只想拥有在给定数组中具有所有索引值的文档,例如[1,2,4]
使用上面的示例,我想要以下 output:
{text: 'random text 1', indexes:[1,2] },
{text: 'random text 3', indexes:[2,4] },
[1,2]
在[1,2,4]
-> OK
[1,3]
不在[1,2,4]
中,因为 3 ->不行
[1,4]
在[1,2,4]
-> OK
任何想法? 感谢您的回答: :)
您可以使用此$match
阶段之一:
$expr
:{
"$match": {
"$expr": {
"$setIsSubset": ["$indexes",[1,2,4]]
}
}
}
这里的例子
$elemMatch
和双重否定( $not
和$nin
):{
"$match": {
"indexes": {
"$not": {
"$elemMatch": {
"$nin": [1,2,4]
}
}
}
}
}
这里的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.