I am currently struggling with this topic and I tried multiple solution from this platform like this one and found no results.
Basically I have an endpoint in a nestjs application that receives via a POST request a from
and a to
parameter that are both dates(string that can be converted with new Date(from/to)
).
I am trying to get all documents in the database between those two dates(from
and to
).
Here is the query that I am making:
const data = await model.find( {
payload: {
timestamp: {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
}
} );
The response that I get is this error:
"Cast to date failed for value \"{ '$gte': 2023-01-03T14:15:52.767Z, '$lte': 2023-01-03T14:16:52.767Z }\" (type Object) at path \"timestamp\" for model \"data\"",
I also tried to get put the timestamp key outside the payload object so:
With error:
{payload:{timestamp: ... }}
query:
const data = await model.find( {
payload: {
timestamp: {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
}
} );
Without error but I get an empty array every time:
{payload:{}, timestamp: ...}
query:
const data = await model.find( {
timestamp: {
$gte: new Date( from ).toISOString(),
$lte: new Date( to ).toISOString()
}
} );
What exactly am I doing wrong?