[Solved] Need some help with a query

I have a collection, with models that look like this:

{
  name: "A",
  buildOrder: [
    { name: "AA", time: 123 }, 
    // ... many of these
  ]
}

I want to select all of the models such that given an array of names, return all models that match all of the array of names.

For example, an input might be [“AA”, “AA”, “AA”, “BB”]. That means the model must have a buildOrder that has 3 objects in it with a name of “AA”, and a “BB”.

The problem I’m having is the multiple matches requirement. Right now my query looks like:

Build.find({
        buildOrder: {
            $elemMatch: {
                time: {
                    $lt: parseInt(time, 10)
                },
                unit: {
                    $in: selectedUnits
                }
            }
        }
    });

But this misses the requirement of matching all the selected units.

It appears there’s an $all selector that I just glossed over, and I think that’s going to do what I want. :smile:

2 Likes