1

I want to find any entries in my collection 'groups' where the id of the object in the array 'games' is in an array of ids.

doc example

The logic in JS:

for (const game in games) {
  return game.find(field => field.id.indexOf(arrayOfIds));
}

And my idea of logic for mongoose:

db.groups.find({ "games.id": { $in: ["5a945...", "1701fa..."] } });
2
  • 1
    Could you edit your question and paste sample documents and expected output ? Commented May 7, 2018 at 16:10
  • @mickl updated it. Is it better to understand now? Commented May 7, 2018 at 17:00

1 Answer 1

1

you should use $elemMatch to match the array

    db.collection.find({
  "games": {
    $elemMatch: {
      id: {
        $in: ["5a945...", "1701fa..."]
      }
    }
  }
})

https://mongoplayground.net/p/cmphODhmGJg

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

1 Comment

That worked! But only when I delete the ObjectId notation from the strings in the array...

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.