In MongoDB I have a collection of documents like this:
{
name: "john",
animals: [...arrayOfAnimals],
cars: [...arrayOfCars],
}
where an example of an animal would be:
{
animal: "horse",
age: 15,
name: "bob",
}
and where an example of a car would be:
{
manufacturer: "nissan",
year: 2015,
convertible: false,
}
How would you filter the collection to get a list of documents that matches all the partial input objects?
e.g.
if I have two objects A and B that are partial animals, and two objects C and D that are partial cars, then this object would be returned as there is a match on all the inputs:
{
name: "mike",
animals: [
{A, missing properties},
{B, missing properties},
...other animals
],
cars: [
{C, missing properties},
{D, missing properties},
...other cars
],
}
but this object would not be returned because it does not have a match on B:
{
name: "jenny",
animals: [
{A, missing properties},
...other animals
],
cars: [
{C, missing properties},
{D, missing properties},
...other cars
],
}
I can already filter for matches of full child objects with $elemMatch but can’t get it to work for matches of partial objects.

