通过Java执行Mongo类查询(JSON)

时间:2021-10-07 12:36:25

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:

我想知道是否有一种方法可以通过Java直接执行mongo类查询,例如,我们将mongoDB类查询作为字符串,作为mongoDB的Java驱动程序中的函数,作为字符串对象返回DBCursor对象。喜欢的东西:

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.

注意:executeQuery()不是一个内置函数。它只是用于演示目的。那么,java api中是否有将json字符串转换为BasicDBObject实例的函数?谢谢。

5 个解决方案

#1


7  

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.

这里展示的不是JSON,而是嵌入MongoDB Shell的Javascript代码。如果出于某种原因需要在Java环境中执行代码,就必须嵌入Javascript引擎(比如Rhino)并实现兼容的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

否则,您只需将JSON转换为DBObject,您可以使用JSON.parse()方法或任何其他JSON映射库,如Jackson。注意,MongoDB使用的是扩展的数据类型集合,这些数据类型在JSON中不存在:http://www.mongodb.org/display/DOCS/Data+ types +和+约定

UPD: Scott Hernandez pointed out about JSON.parse.

Scott Hernandez指出了关于JSON.parse的事情。

#2


3  

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.

您还可以使用com.mongodb.util。JSON,但我不推荐它。它是更少的描述性。

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.

请注意,这可能容易受到SQL注入的影响,因为您自己解析/构建过滤器。

I recommend using Jongo's parameterized query.

我建议使用Jongo的参数化查询。

#3


1  

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

看一下Jongo库——它允许您使用命令行语法运行非常高级的查询。

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.

它还使用一个非常快速的GSON映射器将您自己的对象作为查询的结果返回给您,而不是一个BasicDBObjects列表。

#4


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

我建议使用类似于mongoshell的查询实用工具(jar)。它允许您使用与mongo命令shell相同的语法编写java(或scala)代码中的mongo查询。更具体地说,您可以用字符串格式编写mongo查询。它还支持高级特性,如聚合管道。看看https://github.com/equalexperts/mongodb -like-query ?

#5


0  

You can follow the example in the tutorial:

您可以参考教程中的示例:

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

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

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

注意,它们使用的是(基本的)DBObject来创建查询,而不是字符串。

#1


7  

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.

这里展示的不是JSON,而是嵌入MongoDB Shell的Javascript代码。如果出于某种原因需要在Java环境中执行代码,就必须嵌入Javascript引擎(比如Rhino)并实现兼容的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

否则,您只需将JSON转换为DBObject,您可以使用JSON.parse()方法或任何其他JSON映射库,如Jackson。注意,MongoDB使用的是扩展的数据类型集合,这些数据类型在JSON中不存在:http://www.mongodb.org/display/DOCS/Data+ types +和+约定

UPD: Scott Hernandez pointed out about JSON.parse.

Scott Hernandez指出了关于JSON.parse的事情。

#2


3  

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.

您还可以使用com.mongodb.util。JSON,但我不推荐它。它是更少的描述性。

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.

请注意,这可能容易受到SQL注入的影响,因为您自己解析/构建过滤器。

I recommend using Jongo's parameterized query.

我建议使用Jongo的参数化查询。

#3


1  

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

看一下Jongo库——它允许您使用命令行语法运行非常高级的查询。

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.

它还使用一个非常快速的GSON映射器将您自己的对象作为查询的结果返回给您,而不是一个BasicDBObjects列表。

#4


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

我建议使用类似于mongoshell的查询实用工具(jar)。它允许您使用与mongo命令shell相同的语法编写java(或scala)代码中的mongo查询。更具体地说,您可以用字符串格式编写mongo查询。它还支持高级特性,如聚合管道。看看https://github.com/equalexperts/mongodb -like-query ?

#5


0  

You can follow the example in the tutorial:

您可以参考教程中的示例:

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

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

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

注意,它们使用的是(基本的)DBObject来创建查询,而不是字符串。