I am simply trying to count some documents. When using other parameters such as “type”: “someType”, I get the correct count. But when I use {"createdAt": {"$gte": "", "$lte": ""}}
(with actual dates which are JS date objects) I get 0 as the count. Been at this for days now, and I can’t seems to find the solution…
I tried narrowing the case down to the smallest reproducable possible. Like so:
const query = {
createdAt: {"$gte": new Date("2023-11-01"), "$lte": new Date("2023-11-30")},
}
const result = await Entity.countDocuments(query)
This returns 0, even though I know for 100% that I have documents matching the critera, with e.g. “createdAt”: ‘2023-11-06T08:09:07.000Z’.
Trying the same countDocuments() function with other parameters returns correct number, for instance:
const query = {
tags: {"$nin":["false-positive", "out-of-scope"]},
}
const result = await Entity.countDocuments(query)
The above will return the correct number of documents which does not have these two strings in its “tags”-field.
I have also tried using the “$and” operator, like so:
const testQuery = {
$and: [{ createdAt: { $gte: new Date("2023-10-01") } }, { createdAt: { $lte: new Date("2023-11-31") } }],
}
But with no luck.
I have also tried logging out the query with the operators, and pasting it into MongoExpress “Advanced”-tab. This returns the correct documents, so the issue seems to be with Mongoose or Node.js.
I have no idea why it is not working with the “$gte” and “$lte”, please help.