Using java driver for complex $and queries

961 views
Skip to first unread message

shabl

unread,
Jan 23, 2012, 11:35:15 AM1/23/12
to mongodb-user
Hi:

I'm trying to dynamically create a mongo query, based off a client
friendly string. I am parsing this string and using the QueryBuilder
class to build this query dynamically. From this API, I do see a way
to chain complex OR statements together, but I do not see an obvious
solution on how to chain complex AND statements. Below is an example
of a query in which I cannot find a suitable solution that the
QueryBuilder API can fulfill? Am I missing something or viewing this
problem in the wrong way? Ideally, I would like to hit our collection
just once. I could break this query into multiple queries and build
the result off of that, but that is not what I want to do.

Example:

(corrId : ab or tenant : 1) and (field1: test1 or field2: test2)


(A : 1 or B : 2 or C : 3) and (D : 4 or E : 5)

Scott Hernandez

unread,
Jan 23, 2012, 1:17:43 PM1/23/12
to mongod...@googlegroups.com
I believe the QueryBuilder interface needs to be updated to support
$and and you are not off target. Please create a jira issue if there
isn't one to address this... patches are welcome too :)

The QueryBuilding is just a helper class but you can create the query
yourself using DBObject or BasicDBObjectBuilder for example.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>

Wes Freeman

unread,
Jan 25, 2012, 6:29:15 PM1/25/12
to mongod...@googlegroups.com
I implemented $and, and submitted a pull request (already merged into mongodb:master): https://github.com/mongodb/mongo-java-driver/pull/55

Which allows you to do something like this to match your query:
DBObject q = QueryBuilder.start()
            .or( new BasicDBObject( "a" , 1 ) , 
                 new BasicDBObject( "b" , 2 ) ,
                 new BasicDBObject( "c" , 3 ) )
            .and( QueryBuilder.start().or(new BasicDBObject( "d" , 4 ), new BasicDBObject( "e" , 5 )).get() ) // yes, I know this is kind of ugly...
            .get();

I like the C# Query syntax better, but this is still a bit better than building it entirely by hand with BasicDBObject.

Scott Hernandez

unread,
Jan 25, 2012, 9:01:39 PM1/25/12
to mongod...@googlegroups.com
K, I added a jira for you to track/get-feedback on:
https://jira.mongodb.org/browse/JAVA-511

> --
> You received this message because you are subscribed to the Google Groups
> "mongodb-user" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/mongodb-user/-/Uc0Hmjb4UAIJ.

shabl

unread,
Jan 26, 2012, 2:25:44 PM1/26/12
to mongodb-user
Thanks guys for the quick turnaround. I will give it a shot.

shabl

unread,
Jan 30, 2012, 2:02:42 PM1/30/12
to mongodb-user
Hi:

I see that this has been committed to master branch. I also see that
there is some discussion on github, with regards on what is the best
way to implement this functionality. I have downloaded the source and
built the jars and everything looks good on our end, at least for our
needs. I am curious, if there is a mvn repo I could point our pom
file too, so that we could pick the master branch when doing our
builds?? If so, could you give that information (version??).

Thanks,

On Jan 25, 8:01 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages