Date request using jongo driver for mongo

I am using jongo driver to connect to my mongoDB.

The query syntax - for example, age less than 18 - is

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

but how to request a date?

In mongoDB, a pair of date key values ​​is stored as

 {"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 when running 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 should not be explicitly converted to the corresponding string, but the syntax for searching using dates is different.

Anyone who has jongo knowledge who can help?

+4
source share
2 answers

Using java.util.Date is the standard way to request a date with Jongo:

 collection.find("{date: {$lt : #}}", new Date(2012, 11, 30)); 
+9
source

Request by passing several parameters -

 jongoCollection.find("{date : { $gte : #, $lte : # }}", new Date(1234567890), new Date(1234567890)) 
0
source

All Articles