How to use the OR Operator in the Java API?

255 views
Skip to first unread message

Roy

unread,
Feb 25, 2011, 8:06:53 AM2/25/11
to mongodb-user
Hello,

I am trying to create the following query using the mongoDB java API:

SELECT * FROM TABLE
WHERE A IN ("Hello") OR B IN ("Hello")

DBObject query = QueryBuilder.start("A").in("hello").or(new
BasicDBObject("$in", new BasicDBObject("B","Hello"))).get();

However it doesn't seem to work so I'm obviously abusing the Or
operator.

Roy

unread,
Feb 25, 2011, 9:25:48 AM2/25/11
to mongodb-user
Found the correct syntax.
DBObject A= QueryBuilder.start("A")in("hello").get();
DBObject B= QueryBuilder.start("B")in("hello").get();

DBObject query = QueryBuilder.start().or(A, B).get();

Akash Gangil

unread,
Nov 28, 2011, 10:11:13 AM11/28/11
to mongod...@googlegroups.com
Does the above query for "OR" work? I have been trying it. I am getting the following error.
Exception in thread "main" com.mongodb.MongoException: invalid query
    at com.mongodb.MongoException.parse(MongoException.java:82)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:310)
    at com.mongodb.DBCursor._check(DBCursor.java:360)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:490)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:515)
    at com.example.mongotest.HelloWorld.main(HelloWorld.java:124)

Scott Hernandez

unread,
Nov 28, 2011, 10:29:46 AM11/28/11
to mongod...@googlegroups.com
What server version are you using?

> --
> 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/-/e1mOBVR4_moJ.
> 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.
>

Reply all
Reply to author
Forward
0 new messages