Complex AND-OR Query

178 views
Skip to first unread message

Chris Bunney

unread,
Apr 23, 2012, 11:50:51 AM4/23/12
to mor...@googlegroups.com
Hi,

I've been trying to combine the and() and or() methods of the Query interface to create a set of conditions where there are 2 lists of criteria, and at least one from each must be satisfied.

I read this discussion and have been trying to use the Query.and() to combine my two $or clauses.

Essentially, I'm trying to say:

    Criteria[] arrayA;
    Criteria[] arrayB;
   
    // Programatically populate both arrays

    Query q = dao.createQuery().and(
        q.or(arrayA),
        q.or(arrayB)
    );

I'm using arrays of criteria because I have to loop through several different inputs to generate the particular criteria I need, and this approach works when I'm just using a single $or, but I can't get Morphia to generate the query I expect when I try and include both $or clauses in the $and as I explained above. I find that there's no $and query and the second $or has overwritten the first, just as if I had simply called or() twice.

E.g I expect something like this:

{
"$and": {
    "0": {
        "$or": {
            "0": //Some criteria,
            "1": //Some criteria,
            "2": //Some criteria,
        }
    },
    "1": {
        "$or": {
            "0": //Some other criteria,
            "1": //Some other criteria,
            "2": //Some other criteria,
        }
    }
}


However, I just get something like this:
{
"$or": {
            "0": //Some other criteria,
            "1": //Some other criteria,
            "2": //Some other criteria,
        }
}


I can't see much documentation, but looking at the test case, this seems to be the right way to go about this. Can anyone help shed any light on why this isn't working as I expect?

Many thanks,

Chris Bunney

unread,
Apr 23, 2012, 12:03:46 PM4/23/12
to mor...@googlegroups.com
Looks like Google mangled the link...

The discussion I referred to was this one: https://groups.google.com/d/topic/morphia/VQemTMBpQ2Y/discussion

Gavin Hogan

unread,
Apr 23, 2012, 9:44:49 PM4/23/12
to mor...@googlegroups.com
Not sure if this helps, but I know we had issues with complex nesting
on mongo 1.8, when we upped to 2.0 things worked fine.

G

Chris Bunney

unread,
Apr 24, 2012, 5:19:09 AM4/24/12
to mor...@googlegroups.com
Thanks for the response, we're already using the latest version of the
Java driver (2.7.3) and version 2.0.2 of mongodb itself. I had a look
through the release notes and JIRA for anything that might have changed
since those versions, but can't see anything relevant.

Chris Bunney

unread,
Apr 30, 2012, 9:46:41 AM4/30/12
to mor...@googlegroups.com
So I've come back to this after working on something else.

I've checked out the Morphia test code and that all works fine on my
machine, tests pass and produce expected results. I tried duplicating my
scenario by creating or clauses using the or(Criteria[]) method, but
couldn't reproduce the results I'm seeing in my actual code.

So, I've stripped out everything in my code in an attempt to get
something that works and starts from there, but I still get the wrong query.

I've have created a test project (Mavenised Eclipse project) that
demonstrates the issue when I run it. I'd really appreciate it if anyone
could take a look at it and see if it works correctly for them, or if
they can spot the issue.

When I run Test.java, I get this query printed to standard out (and
confirmed via server profiling):

{ "$or" : [ { "map.field2" : { "$exists" : true}}]}

but I'm expecting something like this:

{ "$and" : [
{"$or" : [ { "map.field1" : { "$exists" : true}}] },
{"$or" : [ { "map.field2" : { "$exists" : true}}]}
] }

Regards,
test.zip

Chris Bunney

unread,
May 1, 2012, 7:18:01 AM5/1/12
to mor...@googlegroups.com
Problem solved: Support for $and is in 0.99.1-SNAPSHOT, not the latest
stable, 0.99, which is what we've been using up to this point

Regards,
Reply all
Reply to author
Forward
0 new messages