mongodb - Command line query within Java -



mongodb - Command line query within Java -

i'm accessing mongodb , want reuse typical command line queries within java. know possible utilize basicdbobject, want utilize command line queries this:

db.mycollection.find()

i tried using command() method of 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 returns me next result.

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

how can run command line query within java?

i not want utilize dbcollection class - because it's not anymore possible run queries different collections.

dbcollection collection = db.getcollection("mycollection"); collection.find(); //not

i don't think can that. db.command() limited these commands. maybe work (i'm having problems getting expected results)

final dbobject command = new basicdbobject(); command.put("eval", "function() { homecoming db." + collectionname + ".find(); }"); commandresult result = db.command(command);

btw, why don't utilize chained calls db.getcollection(collectionname).find(); avoid sticking 1 collection?

java mongodb generic-collections

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -