1

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
Cebe
  • 498
  • 6
  • 10
  • 1
    Just create the dates "before" you send them to the query. You only need to calculate when working with the value present in the document. And those are "aggregation pipeline" operators, not "query" operators. – Neil Lunn Jun 07 '17 at 14:10
  • Thank you Neil, I edited my question, adding the query I actually use and not the query I used to debug. Also, I don't see this as a duplicate: I previously saw the question you link it to but it still doesn't work if I do something like: $lt: new ISODate(createdOn) + 24 * 60 * 60 * 1000 -- I think it has to do with ISODate(createdOn) – Cebe Jun 07 '17 at 14:25
  • Please read things carefully. See this and at least understand `{ "$gte": new Date(createdOn), "$lt": new Date(new Date(createdOn).valueOf() + (1000 * 60 * 60 * 24)) }`. The "parameters" you supply as query arguments are evaluated **"before"** they are sent to MongoDB for processing. You don't want to and do not need to have server based calculations performed on a local variable. You instead evaluate the results which sends the correct arguments to the query.Still a duplicate of the question. Pass in the correct parameter value is should have learned by actually reading the answers there. – Neil Lunn Jun 08 '17 at 00:20
  • I did come up with the 2 query arguments, but wanted to understand why my first attempt was wrong. I still think too much in SQL terms I guess. Thank you, I'm learning along the way. – Cebe Jun 08 '17 at 09:52

0 Answers0