Using Mongoose (or even just MongoDB queries), I'd like to return results for a query that matches all the documents where both:
a) the "units" field (an array of subdocuments) contains a subdocument with a "unit" field, which itself contains a subdocument with an "_id" field that matches a given string value,
b) the "units" field contains a subdocument with a "period" field (an array of Date objects) where a given date falls between the first and second elements in the array.
The data structure looks like this:
{
"_id" : ObjectId("5984bdfdb3ac279e39f156d4"),
"surname" : "Dare",
"firstname" : "Dan",
"units" : [{
"period" : [
ISODate("2018-01-01T00:00:00.000Z"),
ISODate("2019-12-31T00:00:00.000Z")
],
"unit" : {
"unit_name" : "My test unit",
"_id" : "5979437c6d346eb7d074497a"
}
}]
}
I've tried using various combinations of .find() and .aggregate(), for example using $project and $filter on the period array, following $elemMatch on the unit._id, but to no avail - I get errors such as "can't use $filter on an Array".
Any pointers in the right direction would be appreciated - at least in terms of the most appropriate query type and most efficient way to combine the operators for producing the dataset I'm after.
The schema (as requested):
{
surname: {
type: String
},
firstname: {
type: String
},
units: {
type: [{
unit: {
_id: String,
unit_name: String,
},
period: [Date]
}]
}
}