mongodbgroovyjongo

Query Date in Jongo


I want to use Jongo for date queries. The user should be able to enter a string with the query, so I want to use the find method with a string. I am using groovy.

My code:

jongo.getCollection("mycollection").find("{birthday: {\$lt : ISODate(\"2012-11-23T00:13:00.000Z\")}}")

I get this exception:

java.lang.IllegalArgumentException: Cannot parse query: {birthday: {$lt : ISODate("2012-11-23T00:13:00.000Z")}}
Error |
at org.jongo.query.BsonQueryFactory.createQuery(BsonQueryFactory.java:162)
Error |
at org.jongo.Find.<init>(Find.java:47)
Error |
at org.jongo.MongoCollection.find(MongoCollection.java:84)
Error |
at org.jongo.MongoCollection.find(MongoCollection.java:80)

What am I doing wrong?


Solution

  • Yep found the relevant line in the source code to confirm

    "ISODate" has nothing to do with this, it's a JavaScript function name in the mongo shell.

    Jongo is using the MongoDB Extended JSON standard for parsing. So you would do

    `{ \$date: \"2012-11-23T00:13:00.000Z\" }`
    

    instead.

    Really, use the quotes the other way around to make it cleaner:

    .find("{ 'birthday': { '$lt': { '$date': '2012-11-23T00:13:00.000Z' } } }")