Oops my bad ! I went a little too fast on this one !
You need a $elemMatch to make sure that your filter applies to the same array element.
I tested again with a few examples to prove it’s working fine this time.
test [direct: primary] test> db.coll.drop()
true
test [direct: primary] test> db.coll.insertMany([{Children: [{DateOfBirth: "2010-05-05"}, {DateOfBirth: "2012-06-06"}]},{Children: [{DateOfBirth: "2008-02-02"}]}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId("638a6ffcbd6d1ee1c17b6ce2"),
'1': ObjectId("638a6ffcbd6d1ee1c17b6ce3")
}
}
test [direct: primary] test> db.coll.find()
[
{
_id: ObjectId("638a6ffcbd6d1ee1c17b6ce2"),
Children: [ { DateOfBirth: '2010-05-05' }, { DateOfBirth: '2012-06-06' } ]
},
{
_id: ObjectId("638a6ffcbd6d1ee1c17b6ce3"),
Children: [ { DateOfBirth: '2008-02-02' } ]
}
]
test [direct: primary] test> db.coll.aggregate([
... {
... '$set': {
... 'Children': {
... '$map': {
... 'input': '$Children',
... 'in': {
... 'DateOfBirthDateTime': {
... '$cond': [
... {
... '$ne': [
... '$$this.DateOfBirth', ''
... ]
... }, {
... '$toDate': '$$this.DateOfBirth'
... }, null
... ]
... },
... 'Name': '$$this.Name'
... }
... }
... }
... }
... }, {
... '$match': {
... 'Children': {
... '$elemMatch': {
... 'DateOfBirthDateTime': {
... '$gte': new Date('Sun, 01 Jan 2017 00:00:00 GMT'),
... '$lte': new Date('Wed, 01 Jan 2020 00:00:00 GMT')
... }
... }
... }
... }
... }
... ])
test [direct: primary] test> db.coll.aggregate([ { '$set': { 'Children': { '$map': { 'input': '$Children', 'in': { 'DateOfBirthDateTime': { '$cond': [ { '$ne': [ '$$this.DateOfBirth', ''] }, { '$toDate': '$$this.DateOfBirth' }, null] }, 'Name': '$$this.Name' } } } } }, { '$match': { 'Children': { '$elemMatch': { 'DateOfBirthDateTime': { '$gte': new Date('Sun, 01 Jan 2007 00:00:00 GMT'), '$lte': new Date('Wed, 01 Jan 2009 00:00:00 GMT') } } } } }])
[
{
_id: ObjectId("638a6ffcbd6d1ee1c17b6ce3"),
Children: [ { DateOfBirthDateTime: ISODate("2008-02-02T00:00:00.000Z") } ]
}
]
test [direct: primary] test> db.coll.aggregate([ { '$set': { 'Children': { '$map': { 'input': '$Children', 'in': { 'DateOfBirthDateTime': { '$cond': [ { '$ne': [ '$$this.DateOfBirth', ''] }, { '$toDate': '$$this.DateOfBirth' }, null] }, 'Name': '$$this.Name' } } } } }, { '$match': { 'Children': { '$elemMatch': { 'DateOfBirthDateTime': { '$gte': new Date('Sun, 01 Jan 2009 00:00:00 GMT'), '$lte': new Date('Wed, 01 Jan 2011 00:00:00 GMT') } } } } }])
[
{
_id: ObjectId("638a6ffcbd6d1ee1c17b6ce2"),
Children: [
{ DateOfBirthDateTime: ISODate("2010-05-05T00:00:00.000Z") },
{ DateOfBirthDateTime: ISODate("2012-06-06T00:00:00.000Z") }
]
}
]
test [direct: primary] test> db.coll.aggregate([ { '$set': { 'Children': { '$map': { 'input': '$Children', 'in': { 'DateOfBirthDateTime': { '$cond': [ { '$ne': [ '$$this.DateOfBirth', ''] }, { '$toDate': '$$this.DateOfBirth' }, null] }, 'Name': '$$this.Name' } } } } }, { '$match': { 'Children': { '$elemMatch': { 'DateOfBirthDateTime': { '$gte': new Date('Sun, 01 Jan 2007 00:00:00 GMT'), '$lte': new Date('Wed, 01 Jan 2011 00:00:00 GMT') } } } } }])
[
{
_id: ObjectId("638a6ffcbd6d1ee1c17b6ce2"),
Children: [
{ DateOfBirthDateTime: ISODate("2010-05-05T00:00:00.000Z") },
{ DateOfBirthDateTime: ISODate("2012-06-06T00:00:00.000Z") }
]
},
{
_id: ObjectId("638a6ffcbd6d1ee1c17b6ce3"),
Children: [ { DateOfBirthDateTime: ISODate("2008-02-02T00:00:00.000Z") } ]
}
]
Cheers,
Maxime.