1

I'm looking for a solution since 3 hours and I don't get it yet.

I have the following collection:

{text: 'random text 1', indexes:[1,2] },
{text: 'random text 2', indexes:[1,3] },
{text: 'random text 3', indexes:[2,4] },

and I would like to have only the documents that have all the index values in a given array like for example [1,2,4]

using the example above, I would like to have the following output:

{text: 'random text 1', indexes:[1,2] },
{text: 'random text 3', indexes:[2,4] },

[1,2] is in [1,2,4] -> OK

[1,3] is not in [1,2,4] because of 3 -> Not OK

[1,4] is in [1,2,4] -> OK

Any idea? thanks for your answer! :)

1 Answer 1

1

You can use one of this this $match stage:

{
  "$match": {
    "$expr": {
      "$setIsSubset": ["$indexes",[1,2,4]]
    }
  }
}

Example here

  • Using $elemMatch and double negation ($not and $nin):
{
  "$match": {
    "indexes": {
      "$not": {
        "$elemMatch": {
          "$nin": [1,2,4]
        }
      }
    }
  }
}

Example here

Sign up to request clarification or add additional context in comments.

1 Comment

Your welcome, also check the update, using $expr and $setIsSubset seems more clear solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.