'mongodb - how to filter multiple column

I have some documents in my collection like below

{
  "_id": "xxxxxxxxx",
  "Keyword Group A": [
    "Facebook",
    "Twitter"
  ],
  "Keyword Group B": [
    "Netflix"
  ]
},
 {
  "_id": "yyyyyyy",
  "Keyword Group A": [
    "Twitter"
  ],
  "Keyword Group B": [
    "Instagram",
  ],
  ...
}

I can fetch all documents whose Keyword Group A contains "Facebook", by below filter

    $filter: {
      input: "$Keyword Group A",
      as: "group",
      cond: { $eq: ["$$group", "Facebook"] },
    }

But If I want to fetch all documents if Keyword Group A contains "Facebook" OR Keyword Group B contains "Instagram", how should I adjust the filters?



Solution 1:[1]

I am not sure why you need to use $filter because your requirement will fulfill just by equal to a condition in $match stage or in query,

  {
    $match: {
      $or: [
        {
          "Keyword Group A": "Facebook"
        },
        {
          "Keyword Group B": "Instagram"
        }
      ]
    }
  }

Playground

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 turivishal