range based queries on Date - java driver

2,855 views
Skip to first unread message

Samantha Berger

unread,
Jul 6, 2011, 11:13:32 AM7/6/11
to mongodb-user
Can we make range queries over date data type? I am trying to execute
the following over the command line (shell):

db.customerdb.find({"dob":{"gte":"2011-01-11T15:42:00Z"}});

returns 0, although there are more than dozens of records.

If I find all the records using shell prompt
db.customerdb.find();

returns all the records, one of them look like:
{ "_id" : ObjectId("4e147469f20685e692966a04"), "createdOn" :
ISODate("2011-07-06T14:42:49.458Z"), "custName" : "xyz", "custType" :
"somecusttype", "dob" : ISODate("2011-01-11T15:42:00Z"),
"updateSince" : ISODate("2011-01-06T15:42:00Z") }

Since i am using java api to access the mongo db from the web
container, therefore it would be interesting to know how to the
execute date based range queries (a defined above).

Thanks in advance,
Sam

Scott Hernandez

unread,
Jul 6, 2011, 11:16:17 AM7/6/11
to mongod...@googlegroups.com
gte is missing "$"

http://mongodb.org/display/DOCS/Advanced Queries

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Samantha Berger

unread,
Jul 6, 2011, 11:27:25 AM7/6/11
to mongodb-user
Thanks, it works on command shell

db.customerdb.find({ "dob" : { "$gte" :
ISODate("2011-01-11T15:42:00Z") }});

Still the question about java remains?

Scott Hernandez

unread,
Jul 6, 2011, 11:40:05 AM7/6/11
to mongod...@googlegroups.com
I'm not sure what the question is. Yes, you can do this in java.

http://groups.google.com/group/mongodb-user/browse_thread/thread/175ba954d0e2440d
(look at the second response for an example)

Samantha Berger

unread,
Jul 6, 2011, 12:02:47 PM7/6/11
to mongodb-user
As compare to shell, querying in java is quite different. However I
would like to share how I have done it, and also suggests to post it
under java-driver wiki page

public List<BasicDBObject> query(String query) {

BasicDBObject queryObject = new BasicDBObject();
BasicDBObject predicate = new BasicDBObject("$lte",new Date()); //
could be greater than
queryObject.put("dob", predicate);
DBCursor cur = myCollection.find(queryObject);
List<DBObject> resultCollection = new ArrayList<DBObject>();
try {
while (cur.hasNext()) {
DBObject s = new DBObject(cur.next().toString());
resultCollection.add(s);
}
} catch (Exception e) {
//do something on errors
e.printStackTrace();
}
logger.debug(resultCollection);
return Collections.unmodifiableList(resultCollection);
}

On the command line

db.mycollection.find({ "dob" : { "$lte" :
ISODate("2011-01-11T18:42:00Z") }});


Cheers,
Samantha

On Jul 6, 5:40 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> I'm not sure what the question is. Yes, you can do this in java.
>
> http://groups.google.com/group/mongodb-user/browse_thread/thread/175b...

Scott Hernandez

unread,
Jul 6, 2011, 12:13:05 PM7/6/11
to mongod...@googlegroups.com
You can simplify that code by using BasicDBObjectBuilder and the
toArray() (really an ArrayList) method of the cursor.
Reply all
Reply to author
Forward
0 new messages