簡體   English   中英

mongodb - 如果數組中的一組值在另一個給定數組中,則匹配[帶有聚合]

[英]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階段之一:

{
  "$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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM