My question is relatively simple. I have a collection of documents that look like this
{
_id:"61f52b609f97a100092842f2"
timestampISO:2022-01-29T11:56:14.000 00:00
}
The timestampISO is a date type.
Is there a way to match by date without using ISODate("...") as seen in this answer ? I am trying not to use ISODate('') as I would actually like to save the pipeline as a string in Mongo but that is not important right now.
I would like to have a match like this
$match: {
timestampISO: {
$gte: {
$dateFromString: {
dateString: '2022-01-01T11:56:14.000 00:00',
}
},
$lte: {
$dateFromString: {
dateString: '2022-01-29T11:56:14.000 00:00',
}
},
}
}
which does not work. It seems as though ISODate() seems to be the only way? Is this correct?
Thank you for your time.
CodePudding user response:
ISODate()
is just an alias in the Mongo shell for new Date()
.
{ $dateFromString: { dateString: '2022-01-01T11:56:14.000 00:00' } }
is more or less the same, however you have to use
{
$match: {
$expr: {
$gte: [
"$timestampISO",
{ $dateFromString: { dateString: '2022-01-01T11:56:14 00:00' } }
]
}
}
}
or maybe a bit simpler:
{ $match:
{$expr: {$gte: ["$timestampISO", { $toDate: '2022-01-01T11:56:14.000 00:00' } ] }}
}