Morphia Generating Wrong Query For Date Range?

1,078 views
Skip to first unread message

Chris Bunney

unread,
Mar 2, 2012, 12:41:56 PM3/2/12
to mor...@googlegroups.com
Hi All,

I've Googled around but not found anything that's helped me sort this out.

I have a problem querying a date field to find dates after a certain point. It seems, to me, that Morphia is generating the wrong query.

My collection and its documents were created through Morphia, and the date fields are java.util.Date objects and have been mapped by Morphia to ISODates, e.g.:

@Entity
public final class MyObject{
    private java.util.Date startDate=new Date();
    private java.util.Date endDate=new Date();
}

Maps to:

{
"_id" : ObjectId("4f4ce52f7efcb40f23000000"),
"startDate" : ISODate("2012-03-01T00:00:00Z"),
"endDate" : ISODate("2012-03-01T00:00:00Z"),
}


I'm then querying Mongo using code like:

myObjDao.createQuery().field("startDate").lessThanOrEq(new Date()).fetch();

which, when I log the toString() output of the query, appears to be generating a query like:

{ "startDate" : { "$lte" : { "$date" : "2012-03-02T17:15:05Z"}}}

This query returns no results from my mongo instance's interactive shell, despite there being documents where startDate is less than the supplied date. 
However, this query, which I modified by hand, does return the expected results:

db.MyObject.find({ "startDate" : { "$lte" :ISODate("2012-03-02T17:05:44Z")} })



I have 2 questions:

Why does Morphia produce this query that doesn't match documents that Morphia itself mapped?

What do I need to do in order to make the query return the expected results?


Thanks in advance for any insight you can give,

Chris

Ahmet Alp Balkan

unread,
Mar 2, 2012, 12:45:09 PM3/2/12
to mor...@googlegroups.com
When you use toString on Query, it may be printing 

{ "startDate" : { "$lte" : { "$date" : "2012-03-02T17:15:05Z"}}}

however, it is actually running the query with ISODate and printed query is not for mongo shell. It is a matter of representation, I had the same problem with ObjectId, it was printing {$oid: …} however it was running the query successfully. Something else should be wrong. Just wanted to point out.

Mike L

unread,
Mar 2, 2012, 1:26:11 PM3/2/12
to mor...@googlegroups.com
You can check the mongo log to see the exact query that Morphia is generating and then try that from the shell.

Chris Bunney

unread,
Mar 2, 2012, 2:10:38 PM3/2/12
to mor...@googlegroups.com
Thanks Ahmet, that had crossed my mind, since I didn't find any
reference to that syntax.

Thanks for the pointer Mike, I'll have to wait until Monday to get
back on my development box and check it out further

Chris

Chris Bunney

unread,
Mar 6, 2012, 5:20:44 AM3/6/12
to mor...@googlegroups.com
Thanks for the responses,

After enabling profiling on the Mongo server (cmd:>
db.setProfilingLevel(2) ) I could access the profiling data (cmd:>
db.system.profile.find().forEach(printjson) ) and see that the query
was fine and it was just the string representation generated by
toString() that appeared incorrect.

The issue that prompted the question was eventually tracked back to
data in the database that hadn't been updated to reflect schema
changes (one of downsides of dynamic schemas, you have to be extra
careful when changing them).

Reply all
Reply to author
Forward
0 new messages