I have a collection of documents with each document containing an array of DOB objects.
Ex:
dobs: [
{ dobTxt: “19500512”, “dobId”: 1111 },
{ dobTxt: “19750512”, “dobId”: 1112 }
]
I am trying to write a range query. Since the dobTxt is not a date, I have to first convert this to a date before checking for the range.
I tried the following in a $match aggregate stage and it’s not working. What am I doing wrong?
[
{
$match: {
“dobs.dobTxt”: {
$ne: null,
},
“dobs.dobTxt”: {
$gte: [
{
$dateFromString: {
dateString: “$dobs.dobTxt”,
format: “%Y-%m-%d %H:%M:%S”,
},
},
ISODate(“1950-01-01T00:00:00Z”),
],
$lte: [
{
$dateFromString: {
dateString: “$dobs.dobTxt”,
format: “%Y-%m-%d %H:%M:%S”,
},
},
ISODate(“2022-01-01T00:00:00Z”),
],
},
},
},
]