Question

I am in progress of migration a JPA/MySQL application to mongodb using morphia. I have some queries like

AND DATE(NOW()) > (DATE(created) + 2)

or

AND ( TIMESTAMPDIFF(MINUTE,kickoff,now()) > 1 )

or

AND DATE(ending) = DATE(NOW())

Is there anything similar in morphia or mongodb?

Was it helpful?

Solution

From your question I understand that you ask for a way to create date queries in MongoDB and how would your write that query in Morphia

The Cookbook for date queries can be found here.

As pointed in the above post, you should use a range query for this.

In MongoDB's shell, for your first query, you would write it like this:

// hope I got the date part right XD
db.posts.find({created: {$lt: new Date().getDate() - 2}});

For range queries morphia has 2 ways:

So the first query would become something like this:

myObjDao.createQuery().field("created").lessThan(new Date(System.currentTimeMillis() - 2 * 24 * 3600 * 1000 )).fetch();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top