Say I have the following documents in a collection called Log:
{
"_id": ObjectId("5925e1861e61596a647cf31f"),
"CreatedOn": ISODate("2017-05-24T19:39:50.957Z"),
"LogType": "Error"
},
{
"_id": ObjectId("5925e5a51e61586ae0779d43"),
"CreatedOn": ISODate("2017-05-25T08:57:25.102Z"),
"LogType": "AuthenticationError"
},
{
"_id": ObjectId("5925e5aa1e61586ae0779d4c"),
"CreatedOn": ISODate("2017-05-25T14:57:30.790Z"),
"LogType": "AuthenticationInfo"
},
{
"_id": ObjectId("59263c231e61596ae05c627e"),
"CreatedOn": ISODate("2017-05-26T02:06:27.900Z"),
"LogType": "Info"
}
I have a daily extraction that will gather logs for a specific date, for example 2017-05-25. The process is automated, and as such, I've a mongo DB query like this:
var createdOn = "2017-05-25"; // this is actually passed as an input param
db.getCollection('Log').find({
"CreatedOn": {
$gte: new ISODate(createdOn),
$lt: { $add: [new ISODate(createdOn), 24 * 60 * 60 * 1000] } // add 1 day
}
})
This is not working; clearly the $lt part is faulty, but I can't find how to fix it despite numerous tries. I have a workaround by passing two parameters (the boundaries) instead of just one, but I'd like to know what my mistake is?
Thanks to Neil, I realized I may have over simplified my question. Here is the query I actually use:
db.Log.aggregate(
[
{
$match: {
"CreatedOn": {
$gte: new ISODate(createdOn),
$lt: { $add: [new ISODate(createdOn), 24 * 60 * 60 * 1000] } // add 1 day
}
}
},(...)
Which doesn't work, replaced with
db.Transaction.aggregate(
[
{
$match: {
"CreatedOn": {
$gte: new ISODate(minDate),
$lt: new ISODate(maxDate)
}
}
},(...)
Which does work fine. Still I don't understand why, and it may have to do with me being a mongodb newbie :-) It still doesn't work if I try something like
$lt: new ISODate(createdOn) + 24 * 60 * 60 * 1000
Or even (taken from linked question, doesn't answer mine)
$lt: Date.now() + 24 * 60 * 60 * 1000 // add 1 day