[英]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.