I am using mongodb's async driver 3.0.2 (http://mongodb.github.io/mongo-java-driver/3.0/driver-async/) with Java.
I am trying to find the 10 closest documents to a place. The following query I would use in a mongodb shell to accomplish this:
db.locations.find( { loc :
{ $geoWithin :
{ $centerSphere :
[ [ 40 , -40 ] , 10 / 3963.2 ]
} } } ).limit(10);
I need to though run this in java so created the query below, but when I run it I get this exception:
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.BasicDBObject.
CODE:
BasicDBObject geometery = new BasicDBObject("$centerSphere", asList(
asList(40, -40), 10 / 3963.2));
BasicDBObject operator = new BasicDBObject("$geoWithin", geometery);
BasicDBObject query = new BasicDBObject("loc", operator);
Block<Document> postsBlock = new Block<Document>() {
@Override
public void apply(final Document document) {
System.out.println(document.toJson());
}
};
SingleResultCallback<Void> postsCallback = new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
System.out.println("Operation Finished!");
}
};
try {
collection.find(query).limit(10).forEach(postsBlock, postsCallback);
} catch (Exception exc) {
exc.printStackTrace();
}