title: Mongodb geonear query in Java
body: <p>How can I run this mongodb command geonear in Java </p>
  
  <blockquote>
    <p> db.runCommand ({ geoNear : "mycollection", near : [
    -121.97679901123047,37.557369232177734],   spherical : true, maxDistance : 50 / 3959 /* to radians */ ,  query : {  $or : [
    {"myuserid" : "att"},{"_id":{$in:
    [ObjectId("4fda8fbbe7e96e2d775595d")] }}] }    } );</p>
  </blockquote>
  
  <p>I am trying this in java using Commandresult but getting error.</p>
  
  <p>This is what I did so far and getting Null pointer exception although the command is printing right. I am new to java and database person. </p>
  
  <pre><code>BasicDBObject myCmd = new BasicDBObject(); 
  myCmd.append("geoNear", "mycollection"); 
  double[] loc = {-121.97679901123047,37.557369232177734}; 
  myCmd.append("near", loc); 
  myCmd.append("spherical", true); 
  myCmd.append("maxDistance", (double)50 / 3959 ); 
  System.out.println(myCmd); 
  CommandResult myResults = db.command(myCmd); 
  System.out.println(myResults.toString());
  </code></pre>
  
  <p>Thanks in advance.</p>