Executing Mongo like Query (JSON)through Java
Asked Answered
S

5

22

I was wondering if there is a way to execute mongo like query directly through Java i.e. we give mongoDB like query as a String to a function in Java driver for mongoDB as a String Object and an DBCursor Object is returned. Something like:

import com.mongodb.*;
import java.net.UnknownHostException;
public class ExecuteQuery {
public static void main(String args[]){
    try{
          Mongo m = new Mongo();
          DB db = m.getDB("test");
          DBCollection coll = db.getCollection("first");
          DBObject doc = new BasicDBObject();
          DBCursor cur =coll.executeQuery("db.first.find({"username":"joe"})");
       }
       catch(UnknownHostException e){
          System.out.println(e);
       }
       catch (MongoException.DuplicateKey e) {
          System.out.println("Exception Caught" + e);
       }
}
}

Note: executeQuery() is not a built in function. It is just used for demonstration purposes. So, Is there a function in the java api which converts a json string to a BasicDBObject instance? Thanks.

Shanahan answered 1/2, 2011 at 9:12 Comment(2)
What solution did you use it?? I'm interesting about thisCusick
You can check this link. It might help. #47094063Fleischman
M
9

What you showed here is not JSON, it's Javascript code for embedded MongoDB Shell. If you need for some reason to execute the code inside Java environment you will have to embed Javascript engine (like Rhino) and implement compatible API.

Otherwise you just need to convert JSON to DBObject and you can do this with JSON.parse() method or any other JSON-mapping library like Jackson. Note that MongoDB uses extended set of data types that are not present in JSON: http://www.mongodb.org/display/DOCS/Data+Types+and+Conventions

UPD: Scott Hernandez pointed out about JSON.parse.

Mcarthur answered 1/2, 2011 at 11:21 Comment(2)
There is a JSON.parse() method to convert json string to a DBObject.Proprietor
Thank you for the correction. Yeah I have tried out the JSON parser provided in the MongoDB library for Java. If possible can you please let me know a bit more about Rhino.Shanahan
R
12

Yes, there is way, by passing the filter as a string. Example:

BasicDBObject query = BasicDBObject.parse("{userId: {$gt: \"1\"}}");
FindIterable<Document> dumps = crapCollection.find(query);

You can Also use com.mongodb.util.JSON, but I don't recommend it. It's less descriptive.

DBObject dbObject = (DBObject)JSON.parse("{userId: {$gt: \"1\"}}");

Please notice that this might be vulnerable to SQL injections because you parse/build the filter yourself.

I recommend using Jongo's parameterized query.

Rheo answered 15/9, 2016 at 13:4 Comment(0)
M
9

What you showed here is not JSON, it's Javascript code for embedded MongoDB Shell. If you need for some reason to execute the code inside Java environment you will have to embed Javascript engine (like Rhino) and implement compatible API.

Otherwise you just need to convert JSON to DBObject and you can do this with JSON.parse() method or any other JSON-mapping library like Jackson. Note that MongoDB uses extended set of data types that are not present in JSON: http://www.mongodb.org/display/DOCS/Data+Types+and+Conventions

UPD: Scott Hernandez pointed out about JSON.parse.

Mcarthur answered 1/2, 2011 at 11:21 Comment(2)
There is a JSON.parse() method to convert json string to a DBObject.Proprietor
Thank you for the correction. Yeah I have tried out the JSON parser provided in the MongoDB library for Java. If possible can you please let me know a bit more about Rhino.Shanahan
A
2

Take a look at the Jongo library - it will allow you to run even pretty advanced queries using the command-line syntax.

It also uses a very fast GSON mapper to return your own objects back to you as the result of the query, instead of a list of BasicDBObjects.

Armelda answered 17/3, 2013 at 15:1 Comment(0)
W
1

I would recommend to use mongo-shell-like-query utility (jar). It allows you to write mongo queries in java (or scala) code using the same syntax as that of mongo command shell. To be more specific, you can write mongo queries in string format. It also supports advance features such as aggregation pipeline. Have a look at https://github.com/EqualExperts/mongo-shell-like-query

Winifield answered 19/7, 2013 at 14:5 Comment(0)
P
-1

You can follow the example in the tutorial:

http://www.mongodb.org/display/DOCS/Java+Tutorial#JavaTutorial-GettingASetofDocumentsWithaQuery

Note their use a of (Basic)DBObject to create the query, not a string.

Proprietor answered 1/2, 2011 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.