Re: MongoDB Shell Like Queries in Java Driver

682 views
Skip to first unread message

Stephen Lee

unread,
Sep 18, 2012, 10:18:20 AM9/18/12
to mongod...@googlegroups.com
Hi Anjana,

Regarding the Java driver, check out http://www.mongodb.org/display/DOCS/Java+Tutorial#JavaTutorial-GettingASetofDocumentsWithaQuery, which shows a simple query using the query() method of an DBCollection instance.  Basically, you form the equivalent shell find query using nested DBObject's.  

-Stephen

On Tuesday, September 18, 2012 8:43:44 AM UTC-4, Anjana Fernando wrote:
Hi everyone,

I'm a newbie to MongoDB .. I was playing around with the MongoDB shell, and then I was going through the Java driver .. and I couldn't really find a way to execute a query like "db.things.find({j: {$ne: 3}, k: {$gt: 10} })" from the Java library. I saw the "eval" is there to execute a whole function, but I saw that it is not recommended to use because of some of its limitations. So my requirement is basically, I want an user to dynamically send a query to my  Java application to retrieve some data .. so there should be some querying mechanism, and couldn't really use the Java library methods to build up the query at that time. Greatly appreciate if someone can give me a suggestion on how to do this. 

Cheers,
Anjana.

Rob Moore

unread,
Sep 18, 2012, 10:09:30 PM9/18/12
to mongod...@googlegroups.com

Anjana,

In addition to the 10gen supported driver, there is also an asynchronous driver that I helped to create.  One of the main focuses of the driver is usability, specifically making the developer's intent clear and concise.

While the driver QueryBuilder doesn't look like the shell's version of a query but is still fairly intuitive:

import static com.allanbank.mongodb.builder.QueryBuilder.where
 
DocumentAssignable query = where("f").greaterThan(23).lessThan(42)
.and("g").lessThan(3);

There is a Getting Started guide to help get you familiar with the different parts of the driver and moving in the right direction.

Rob.

Stephen Steneker

unread,
Sep 18, 2012, 10:17:23 PM9/18/12
to mongod...@googlegroups.com
I'm a newbie to MongoDB .. I was playing around with the MongoDB shell, and then I was going through the Java driver .. and I couldn't really find a way to execute a query like "db.things.find({j: {$ne: 3}, k: {$gt: 10} })" from the Java library. I saw the "eval" is there to execute a whole function, but I saw that it is not recommended to use because of some of its limitations. So my requirement is basically, I want an user to dynamically send a query to my  Java application to retrieve some data .. so there should be some querying mechanism, and couldn't really use the Java library methods to build up the query at that time. Greatly appreciate if someone can give me a suggestion on how to do this. 

Hi Anjana,

You may also wish to have a look at the Jongo community-supported driver, whose goal is to "Query in Java as in Mongo Shell": http://jongo.org/ 

Cheers,
Stephen
Reply all
Reply to author
Forward
0 new messages