1

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();
                }

1 Answer 1

4

In your connection you need to specify the codec registry com.mongodb.MongoClient.getDefaultCodecRegistry() should do fine

For the async driver

MongoClientSettings settings = MongoClientSettings.builder().readPreference(readPreference)
    .codecRegistry(com.mongodb.MongoClient.getDefaultCodecRegistry()).socketSettings(sockSettings)
    .connectionPoolSettings(connPoolSettings).credentialList(credentials))
    .clusterSettings(clusterSettings).build();
LOG.info("MongoClientSettings: {}, {}, {}, {}", sockSettings, connPoolSettings, clusterSettings, credentials);
MongoClient mgc = MongoClients.create(settings);

for the normal driver

MongoClientOptions settings = MongoClientOptions.builder().readPreference(readPreference)
    .codecRegistry(com.mongodb.MongoClient.getDefaultCodecRegistry()).build();
MongoClient mgc= new MongoClient(servers,credentials,settings);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.