i’m trying to find between two dates. I have seen other similar questions on Stackoverflow.
My query should return 0 documents but i don’t know why it is returning the document.
field:
"dates": [
{
"$date": "2023-04-08T14:37:02.937Z"
},
{
"$date": "2023-04-15T14:37:02.937Z"
},
{
"$date": "2023-04-23T14:37:02.937Z"
}
],
My query :
{ dates: {$gte:ISODate('2023-04-18'), $lt: ISODate('2023-04-20') } }
This query should return 0 documents but instead it returns the document.
>Solution :
Try using $elemMatch:
db.collection.find({
dates: {
$elemMatch: {
$gte: ISODate("2023-04-18"),
$lt: ISODate("2023-04-20")
}
}
})
See how it works on the playground example