简体   繁体   English

使用Java类执行像查询一样的mongoDB shell

[英]Execute mongoDB shell like query using a java class

Using a java class i want to execute MongoDB shell query stored in a String variable.Currently i am using the following code. 我想使用Java类执行存储在String变量中的MongoDB shell查询。当前我正在使用以下代码。

String query="db.INSTANT.insert( { item: 'card', qty: 12 } )";
MongoClient mongo = new MongoClient("localhost",27017);
DB db = mongo.getDB("mydb");
db.eval(query);

The above code works fine for insert. 上面的代码可以很好地插入。 But i want to execute find statement like query=db.INSTANT.find({"item":"card"}) .Is there any way to execute this query and print the collection set. 但是我想执行find语句,例如query=db.INSTANT.find({"item":"card"}) 。有什么方法可以执行此查询并打印收集集。

Assuming that the function of eval has been deprecated since version 3.0. 假设从版本3.0开始不推荐使用eval的功能。

The helper db.eval() in the Java Driver wraps the mongo eval command, So you can evaluate JavaScript code this way Java驱动程序中的帮助程序db.eval()包装了mongo eval命令,因此您可以通过这种方式评估JavaScript代码

String query="db.INSTANT.find( { item: 'card', qty: 12 } ).toArray()";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM