为什么我会收到以下错误:找不到类 com.mongodb.client.model.geojson.Polygon 的编解码器

问题描述 投票:0回答:1

我正在尝试使用 MongoDB 的

com.mongodb.client.model.geojson.Polygon
类和
com.mongodb.client.model.geojson.Position
类进行地理空间查询。客户端代码填充其自己的boundingBox对象中的四个双角。

这是代码片段:

Polygon polygon = new Polygon(Arrays.asList(new Position(boundingBox.getRightLongitude(),boundingBox.getTopLatitude()), 
                new Position(boundingBox.getLeftLongitude(), boundingBox.getTopLatitude()), 
                new Position(boundingBox.getLeftLongitude(), boundingBox.getBottomLatitude()), 
                new Position(boundingBox.getRightLongitude(), boundingBox.getBottomLatitude()),
                new Position(boundingBox.getRightLongitude(), boundingBox.getTopLatitude())));
        //Document filter = new Document("coordinates",geoWithin("coordinates", polygon));


    Block<Document> printBlock = new Block<Document>() {
         @Override
         public void apply(final Document document) {
             System.out.println(document.toJson());
         }
    };
    database.getCollection("roads").find(geoWithin("coordinates",polygon)).forEach(printBlock);

这是错误:

Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.client.model.geojson.Polygon.
java mongodb geospatial
1个回答
0
投票

在您的设置中,您需要指定编解码器注册表,您当前缺少

GeoJsonCodecProvider()
,使用
com.mongodb.MongoClient.getDefaultCodecRegistry()
应该没问题

对于异步驱动程序

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

对于普通司机

MongoClientOptions settings = MongoClientOptions.builder().readPreference(readPreference)
    .codecRegistry(com.mongodb.MongoClient.getDefaultCodecRegistry()).build();
MongoClient mgc= new MongoClient(servers,credentials,settings);
© www.soinside.com 2019 - 2024. All rights reserved.