0

I have the following document :

{
  "key": [
    [
      "FOO",
      "BAR"
    ],
    [
      "FOO",
      "BAZ"
    ]
  ]
}

I want to retrieve this document when at least one of the array from the key array contains 100% of some values.

Exemples :

+--------------+----------------+
| Given values | Should Match ? |
+--------------+----------------+
| FOO          | Yes            |
| BAR          | Yes            |
| BAZ          | Yes            |
| FOO, BAR     | Yes            |
| FOO, BAZ     | Yes            |
| BAR, BAZ     | No             |
+--------------+----------------+

From the documentation, it looks like using the $elemMatch and $all operator could work, but it doesn't (the following returns nothing) :

{
  "key": {
    "$elemMatch": {
      "$all": [
        "FOO"
      ]
    }
  }
}

Playground : https://mongoplayground.net/p/BtqWjoUkASs

To summarize, I want to run the $all operator on each of the arrays contained in an other array.

Antoine Bellion
  • 136
  • 1
  • 9
  • 1
    Does this answer your question? [Querying an array of arrays in MongoDB](https://stackoverflow.com/questions/12629692/querying-an-array-of-arrays-in-mongodb) – turivishal Aug 31 '20 at 15:33
  • No, I want the `$all` operator to be evaluated on the nested arrays, not on their values. The `$all` operator must run on each of the `key` values. – Antoine Bellion Aug 31 '20 at 15:41
  • 1
    Its same just use `$all` instead of `$in` https://mongoplayground.net/p/KSmGz7E4A7z – turivishal Aug 31 '20 at 15:42
  • This does not work : https://mongoplayground.net/p/-N3XKrSCE5J – Antoine Bellion Aug 31 '20 at 15:44
  • so that should work with `$in` – turivishal Aug 31 '20 at 15:47
  • No : https://mongoplayground.net/p/sEjWdtKtYhy This shouldn't match the document, because one of the specified value (`NOP`) is not within one of the arrays. – Antoine Bellion Aug 31 '20 at 15:49
  • if one of the value matches from the array it will return, I think you have to look at the use of [$in](https://docs.mongodb.com/manual/reference/operator/query/in/) and [$all](https://docs.mongodb.com/manual/reference/operator/query/all/index.html), it will helpful if you edit your question and clear the exact expectation and problem. – turivishal Aug 31 '20 at 15:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220747/discussion-between-antoine-bellion-and-turivishal). – Antoine Bellion Aug 31 '20 at 15:57

0 Answers0