Complex AND-OR query in Morphia
Just guessing (don't have time to test), but should it be:
Query q = dao.createQuery().and( q.or(q.criteria(arrayA)), q.or(q.criteria(arrayB)));
Update You're right, Query.criteria
was not right--it was what was used in the simple test, so I thought you had missed something. This seems to work for me (breaking it into two statements):
Query q = dao.createQuery();q.and( q.or(arrayA), q.or(arrayB));
Update 2 More complete test code:
Criteria[] arrayA = {dao.createQuery().criteria("test").equal(1), dao.createQuery().criteria("test").equal(3)};Criteria[] arrayB = {dao.createQuery().criteria("test").equal(2), dao.createQuery().criteria("test").equal(4)};;Query q = dao.createQuery();q.and( q.or(arrayA), q.or(arrayB));System.out.println(q.toString());
gives:
{ "$and" : [ { "$or" : [ { "test" : 1} , { "test" : 3}]} , { "$or" : [ { "test" : 2} , { "test" : 4}]}]}
Despite Morphia 0.99 including the Query.and(Criteria ...)
method, it doesn't generate the correct query.
Issue 338, which addresses support for explicit $and clauses in queries is targeted at Morphia 0.99.1, which is currently only available as a SNAPSHOT version via Maven.
However, using 0.99.1-SNAPSHOT resolved the issue for us.