Date range search using Java

198 views
Skip to first unread message

mark....@arcinnovations.co.nz

unread,
Mar 24, 2014, 5:08:34 PM3/24/14
to mongod...@googlegroups.com
Hi,

I'm trying to get some data out of a Mongo instance (v 2.4.6) using Java.

I have a date range, which is made of two java.util.Date objects.

I create my query like this:

            Query query = new Query(where("myColumn").is(myValue).andOperator(
                    Criteria.where("myDate").lt(endDate),
                    Criteria.where("myDate").gt(startDate)));

... but I get no data back, even though I know that there is some which matches the selection criteria in there.

The actual query, extracted from the Query object, looks like this:

{ "myColumn" : "myValue" , "$and" : [ { "myDate" : { "$lt" : { "$date" : "2013-12-31T20:56:05.423Z"}}} , { "myDate" : { "$gt" : { "$date" : "2013-11-30T20:56:05.381Z"}}}]}

If I just search on "myColumn" and remove the date parts, data is brought back which includes "myDate" values inbetween the previously included start and end date search criteria.

Thanks for any assistance.

Joanna Cheng

unread,
Mar 24, 2014, 9:33:40 PM3/24/14
to mongod...@googlegroups.com
Hi Mark,

I'm not convinced that syntax is correct. When I try this on my (2.4.9) instance I get:

> db.test.find()
{ "_id" : ObjectId("5330d1c418a8239c40744339"), "myColumn" : "myValue", "myDate" : ISODate("2014-03-25T00:45:56.016Z") }
> db.test.find({ "myColumn" : "myValue" , "myDate" : { "$lt" : { "$date" : "2014-12-31T20:56:05.423Z"}}})
> db.test.find({ "myColumn" : "myValue" , "myDate" : { "$date" : "2014-12-31T20:56:05.423Z"}})
error: { "$err" : "invalid operator: $date", "code" : 10068 }


Cheers,
Joanna 

mark....@arcinnovations.co.nz

unread,
Mar 24, 2014, 10:35:33 PM3/24/14
to mongod...@googlegroups.com
Hi,

All good - the code is correct. Put it down to user error.  :-/

Thanks.

Jeff Yemin

unread,
Mar 24, 2014, 10:41:16 PM3/24/14
to mongod...@googlegroups.com
Hi Mark,

I'm not sure what's happening in your application, but I wrote a simple program showing that this type of range query works properly from Java, but with and without the use of the $and operator:



Regards,
Jeff


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.

---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/c9ca64ac-bf4b-49a1-aa5b-91105d46b05b%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages