I have nested array database records styled as such:
{
"_id" : "A",
"foo" : [
{
"_id" : "a",
"date" : ISODate("2017-07-13T23:27:13.522Z")
},
{
"_id" : "b",
"date" : ISODate("2017-08-04T22:36:36.381Z")
},
{
"_id" : "c",
"date" : ISODate("2017-08-23T23:59:40.202Z")
}
]
},
{
"_id" : "B",
"foo" : [
{
"_id" : "d",
"date" : ISODate("2017-07-17T23:27:13.522Z")
},
{
"_id" : "e",
"date" : ISODate("2017-01-06T22:36:36.381Z")
},
{
"_id" : "f",
"date" : ISODate("2017-09-14T23:59:40.202Z")
}
]
},
{
"_id" : "C",
"foo" : [
{
"_id" : "g",
"date" : ISODate("2017-11-17T23:27:13.522Z")
},
{
"_id" : "h",
"date" : ISODate("2017-06-06T22:36:36.381Z")
},
{
"_id" : "i",
"date" : ISODate("2017-10-14T23:59:40.202Z")
}
]
}
When I run the query:
db.bar.find(
{
$and: [
{"foo.date": {$lte: new Date(2017,8,1)}},
{"foo.date": {$gte: new Date(2017,7,1)}}
]
},
{
"_id":1
}
)
I'm returned
{
_id: "A"
},
{
_id: "B"
},
{
_id: "C"
}
Logically I'm asking for only the records where at least one date is between Aug-1 and Sept-1 (Record A), but am getting all records.
I'm thinking it might be referencing different dates on the subdocuments i.e. where foo.1.date > Aug-1 and foo.0.date < Sept-1.
Has anyone else had issue and found a resolution to this?