title: Command line query within Java
body: <p>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:</p>
  
  <pre><code>db.MyCollection.find()
  </code></pre>
  
  <p>I tried now using the command() method of the database:</p>
  
  <pre><code>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));
  </code></pre>
  
  <p>But this returns me the following result. </p>
  
  <pre><code>"ok": 0,
  "code": 59,
  "errmsg": "no such cmd: db.MyCollection.find()",
  "bad cmd": {"db.MyCollection.find()": true},
  "serverUsed": "localhost:27017"
  </code></pre>
  
  <p><strong>How can I run a command line query within Java?</strong></p>
  
  <p>I do not want to use the DBCollection class - because then it's not anymore possible to run queries for different collections. </p>
  
  <pre><code>DBCollection collection = db.getCollection("MyCollection");
  collection.find(); //NOT THIS
  </code></pre>