Tuesday 6 June 2017

Dates and MongoDB - FAIL

I've found a TON of documentation on working with dates in mongodb. Unfortunately none of it seems to work as it's supposed to. Here's my problem... I'm trying to query by date. This is what my document looks like when I just search with a find():

{
    "_id": ObjectId("5935ff97d1d72d2134d7ad8f"),
    "username": "Test",
    "email": "test@test",
    "password": "sha1$8dbb9bdc$1$b0096596e0e3aba3ad883489bc713b0462ad6472",
    "access": "test",
    "cookies": [{
            "hash": "ZOddh981SZ7uiw3XPQYwJzET18byscWo3BV0RzIc8Eo8QY2VdlVm23qs28AAvxPx",
            "exipires": ISODate("2017-06-12T01:34:45.895Z")
        }, {
            "hash": "gk8voLrc2JtX832GExcWlSoVdjkiEoFAvLQAayevtPJGJ7tCchYAP0it93aAMBlb",
            "exipires": "ISODate(\"2017-06-11 20:37:10.167\")"
        }
    ]   
}

(As you can see, I have 2 cookies. One with escaped quotes around the date, one without.)

And here are the searches I've tried so far:

db.getCollection("auth").find({ 'cookies.expires': {'$gte': {$date: "2017-06-07T01:37:14.362Z"} } })
db.getCollection("auth").find({ 'cookies.expires': {'$lte': {$date: "2017-06-07T01:37:14.362Z"} } })
db.getCollection("auth").find('cookies.expires': { '$gte': new ISODate("2017-06-07T01:37:14.362Z") } })
db.getCollection("auth").find('cookies.expires': { '$lte': new ISODate("2017-06-07T01:37:14.362Z") } })
db.getCollection("auth").find('cookies.expires': { '$gte': new ISODate() } })
db.getCollection("auth").find('cookies.expires': { '$gte': new Date("2017-06-07T01:37:14.362Z") } })
db.getCollection("auth").find('cookies.expires': { '$lte': new Date("2017-06-07T01:37:14.362Z") } })
db.getCollection("auth").find('cookies.expires': { '$gte': new Date("2017-06-07T01:37:14.362Z") } })

I'm pretty positive that the search SHOULD be for $gte, since I want to find all dates in the future. But I've tried both ways just in case it was backwards from what I expected.

None of these work and I can't figure out why - been at this for a good 2 hours now. A fresh set of eyes would be appreciated.



via Ethan

No comments:

Post a Comment