4

I'm accessing a MongoDB and want to reuse typical command line queries also within Java. I know it is possible to use the BasicDBObject, but I want to use command line queries like this:

db.MyCollection.find()

I tried now using the command() method of the database:

MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("MyDatabase");
CommandResult result= db.command("db.MyCollection.find()");
JSONObject resultJson = new JSONObject(result.toString());
System.out.println(resultJson.toString(4));

But this returns me the following result.

"ok": 0,
"code": 59,
"errmsg": "no such cmd: db.MyCollection.find()",
"bad cmd": {"db.MyCollection.find()": true},
"serverUsed": "localhost:27017"

How can I run a command line query within Java?

I do not want to use the DBCollection class - because then it's not anymore possible to run queries for different collections.

DBCollection collection = db.getCollection("MyCollection");
collection.find(); //NOT THIS
Philipp
  • 4,645
  • 3
  • 47
  • 80
  • Why would you want to trade a perfectly save API for one vulnerable to injection attacks? Do you have a good reason for this except personal preference for syntax? – Philipp Oct 15 '14 at 14:22
  • Yes, I would like to offer a small API for our non-java based software - which does not have a mongodb interface. So I need to have a class which can run different queries. – Philipp Oct 15 '14 at 14:36

1 Answers1

6

I don't think you can do that. With db.command() you are limited to these commands. Maybe you could get something like this to work (I'm having problems with getting expected results)

    final DBObject command = new BasicDBObject();
    command.put("eval", "function() { return db." + collectionName + ".find(); }");
    CommandResult result = db.command(command);

BTW, why don't you use chained calls like db.getCollection(collectionName).find(); to avoid sticking to one collection?

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68
  • Getting exception "Caused by: com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on bapdocdb_pricing to execute command { eval:.... on server ...:27017. The full response is { "ok" : 0.0, "errmsg" : "not authorized on bapdocdb_pricing to execute command { eval: \"...\" }", "code" : 13, "codeName" : "Unauthorized" } – myuce Aug 15 '17 at 09:54
  • 2
    @myuce Does [this](https://www.claudiokuenzler.com/blog/555/allow-mongodb-user-execute-command-eval-mongodb-3.x#.WZLIpVUjGUk) help? – Predrag Maric Aug 15 '17 at 10:12
  • Thank you Predrag, went to use the hard way and started using aggregate function :). – myuce Oct 09 '17 at 20:21
  • @PredragMaric now eval is deprecated , any alternative way to do this – Anshul May 16 '20 at 08:24
  • @Anshul Don't know, sorry. Haven't been working with mongodb in a long time. – Predrag Maric May 16 '20 at 18:22
  • @PredragMaric no issues :) – Anshul May 17 '20 at 07:24
  • As answered here https://stackoverflow.com/a/61952470/3603660 eval is removed from 4.2. There is no more way to do this using the Java Driver – loicmathieu May 22 '20 at 14:12