-1

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.

Ethan
  • 787
  • 1
  • 8
  • 28
  • 2
    is that just a typo ? `expires` vs `exipires`. This should work `db.getCollection("auth").find({'cookies.expires': { '$gte': ISODate("2017-06-07T01:37:14.362Z") } })` – s7vr Jun 07 '17 at 02:04
  • Oh. My. God. That's what it is. I can't believe I've wasted so much time on a typo. Thank you thank you!! – Ethan Jun 07 '17 at 02:09
  • 3
    `"ISODate(\"2017-06-11 20:37:10.167\")"` is a string as well and not a `Date` object – Neil Lunn Jun 07 '17 at 03:10

1 Answers1

0
db.getCollection("auth").find({
      'cookies.expires': { '$gte': ISODate("2017-06-07T01:37:14.362Z") } 
})
IsaacBok
  • 424
  • 5
  • 18
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 07 '17 at 13:59
  • In the end it was just a typo. All of the $gt searches I used worked fine... Once I fixed the typo in my document. I'll mark as the answer so it doesn't go unanswered. – Ethan Jun 07 '17 at 15:11