4

I use jongo driver to connect to my mongoDB .

The syntax for querying - for ex, some age less than 18 - is

collection.find("{age: {$lt : 18}}");

but how to query for a date ?

In the mongoDB the date key value pair is stored like

{"date" : ISODate("2012-11-23T00:12:23.123Z")}

so I tried the following:

collection.find("{date: {$lt : ISODate(\"2012-11-23T00:13:00.000Z\")}}");

but I get this exception while running the java code :

Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.IllegalArgumentException: {dateLastSeen: {$lt: ISODate("2012-11-23T00:13:00.000Z")}} cannot be parsed
        at org.jongo.query.Query.convertToDBObject(Query.java:33)
        at org.jongo.query.Query.<init>(Query.java:26)
        at org.jongo.query.QueryFactory.createQuery(QueryFactory.java:38)
        at org.jongo.Find.<init>(Find.java:42)
        ... 10 more
Caused by: com.mongodb.util.JSONParseException:
{dateLastSeen: {$lt: ISODate("2012-11-23T00:13:00.000Z")}}
                     ^
        at com.mongodb.util.JSONParser.parse(JSON.java:198)
        at com.mongodb.util.JSONParser.parseObject(JSON.java:231)
        at com.mongodb.util.JSONParser.parse(JSON.java:195)
        at com.mongodb.util.JSONParser.parseObject(JSON.java:231)
        at com.mongodb.util.JSONParser.parse(JSON.java:195)
        at com.mongodb.util.JSONParser.parse(JSON.java:145)
        at com.mongodb.util.JSON.parse(JSON.java:81)
        at com.mongodb.util.JSON.parse(JSON.java:66)
        at org.jongo.query.Query.convertToDBObject(Query.java:31)

So I think, that dates are not to be plainly converted to the corresponding string, but the syntax to search using date is different.

Anybody with jongo knowledge who can help ?

user1803112
  • 83
  • 1
  • 5

2 Answers2

9

Using java.util.Date is the standard way to query for date with Jongo:

collection.find("{date: {$lt : #}}", new Date(2012, 11, 30));
yves amsellem
  • 7,106
  • 5
  • 44
  • 68
  • So jongo will try to find a variable named future (when using the first approach) ? – user1803112 Nov 29 '12 at 10:48
  • Oups, the two approaches are similar. You have to use a query with parameter; sorry for the confusion (see updated answer). – yves amsellem Nov 29 '12 at 10:56
  • @yvesamsellem I am using **new** parameter in the jongo query. I get the same exception as mentioned in the question. I use it in this way **some_date_field : { $gte : new Date(date_in_milliseconds) }** Please let me know what is wrong with it. It works well in mongo shell. – Vinay Kadalagi Sep 07 '17 at 21:37
  • @yvesamsellem OK it worked.. with your solution. Can you tell how can i pass multiple # parameters to pass 2 dates. Please help – Vinay Kadalagi Sep 07 '17 at 22:10
  • Ok figured this out too - collection.find("{date : { $gte : #, $lte : # }}", new Date(1234567890), new Date(1234567890)) **might help someone** – Vinay Kadalagi Sep 07 '17 at 22:26
0

To query by Passing multiple parameters -

jongoCollection.find("{date : { $gte : #, $lte : # }}", new Date(1234567890), new Date(1234567890))
Vinay Kadalagi
  • 1,315
  • 1
  • 8
  • 11