Java QueryBuilder in MongoDB

185 views
Skip to first unread message

Maulik

unread,
Jul 2, 2011, 10:20:38 PM7/2/11
to mongodb-user
Hi,

I have to implement following logic in my query.

qry.append(" AND ( EXPIRATION_DATE is null OR
trunc(EXPIRATION_DATE) >= :expirationDate) ");
qry.append(" AND ( release_date IS NULL OR
trunc(release_date)<= :publishDate ) ");

How would I perform these operations by using QueryBuilder? Is there
other way around?

Scott Hernandez

unread,
Jul 5, 2011, 7:36:25 AM7/5/11
to mongod...@googlegroups.com
Can you express/run this query in the mongo javascript shell?

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

Sam Millman

unread,
Jul 5, 2011, 7:43:28 AM7/5/11
to mongod...@googlegroups.com
Can you give us the full query you are only showing us a fraction of the SQL query you wish to transfer.

Firstly the trunc() function is not needed since using timestamps you can over come the nessecary overhead of having to split dates up and garnish their "parts".

padmaprasad

unread,
Sep 17, 2012, 10:17:26 AM9/17/12
to mongod...@googlegroups.com
Hi,
 
Could u help me sort out this problem :

mysql
select fieldOne from coll where fieldOne in (2,3);

//WORKING
In mongo shell
db.coll.find({"fieldOne" : { "$in" : [2,3] }})

//NOT WORKING IN JAVA
In Java

(1)
BasicDBObject queryConditions = new BasicDBObject();
DBObject q = QueryBuilder.start("fieldOne").in(2).get();
queryConditions.put("fieldOne", q);
DBCursor cursorConsumers = coll.find(queryConditions);
cursorConsumers.hasNext();

(2)
String[] array = {"2","3"};
BasicDBObject queryConditions = new BasicDBObject();
DBObject q = QueryBuilder.start("fieldOne").in(array).get();
queryConditions.put("fieldOne", q);
DBCursor cursorConsumers = coll.find(queryConditions);
cursorConsumers.hasNext();

(3)
String[] array = {"2","3"};
DBObject q = QueryBuilder.start("fieldOne").in(array).get();
DBCursor cursorConsumers = coll.find(q);
cursorConsumers.hasNext();

I've tried these in java but not working, So any suggestions will be valuable.

Thanks & Regards,
Padmaprasad

Scott Hernandez

unread,
Sep 17, 2012, 10:53:50 AM9/17/12
to mongod...@googlegroups.com
Please create a new issue and don't respond to an old one.

Strings are not numbers. Do this:

Long[] array = {2,3};
DBObject q = QueryBuilder.start("fieldOne").in(array).get();
//print "q.toString()" to see the query before you run it.
DBCursor cursorConsumers = coll.find(q);
//foreach loop here
> --
> 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
> See also the IRC channel -- freenode.net#mongodb

padmaprasad

unread,
Sep 17, 2012, 11:14:22 AM9/17/12
to mongod...@googlegroups.com
Hi Scott,

Thanks for the help,it worked.

Thanks and regards,
Padmaprasad 

padmaprasad

unread,
Sep 20, 2012, 8:53:52 AM9/20/12
to mongod...@googlegroups.com
Hi,

  DBObject query = QueryBuilder.start("fieldA").greaterThan("fieldB").get();
  not working
  I want to query like SELECT * FROM collection WHERE fieldA > fieldB using monogdb in java.

 Thanks 
 Padmaprasad

Scott Hernandez

unread,
Sep 20, 2012, 9:26:30 AM9/20/12
to mongod...@googlegroups.com
That is not possible with the query language, and has nothing to do
with the java builders. You cannot yet compare two fields like that
without using javascript in a $where clause:
http://www.mongodb.org/display/DOCS/Server-side+Code+Execution#Server-sideCodeExecution-%7B%7B%24where%7D%7DClausesandFunctionsinQueries

There is a feature request for something like this but it not scheduled yet.
Reply all
Reply to author
Forward
0 new messages