mongo-operations spring mongo bulk opeartion执行异常(CodecConfigurationException:找不到类的编解码器)

时间:2017-04-30 03:44:06

标签: java spring mongodb spring-boot

我试图使用spring mongo操作类在mongodb中执行批量操作。

这些是我的POJO课程:

@Document(collection = "location_settings_mst")
public class LocationSettingsEntity {
    @Id
    private String id;
    @Field("userId")
    private String userId;
    @Field("location")
    private List<LocationSettingEntity> locationSettings = new LinkedList<>();
    // Getters and Setters
}

public class LocationSettingEntity {
    @Field("name")
    private String name;
    @Field("latitude")
    private double lat;
    @Field("longitude")
    private double lon;
    @Field("geoPoint")
    private String geoPoint;
    // Getters and Setters
}

但是,当我尝试执行以下批量操作时:

// Here mongo is an object of type org.springframework.data.mongodb.core.MongoOperations
BulkOperations bulkOps = mongo.bulkOps(BulkMode.ORDERED, LocationSettingsEntity.class); 
Query query = new Query();
List<LocationSettingEntity> list = new LinkedList<>(); // this list contains LocationSettingEntity objects
query.addCriteria(Criteria.where("userId").is(userId));
Update update = new Update();
update.pushAll("location", list.toArray());
bulkOps = bulkOps.updateOne(query, update);
bulkOps.execute();

我收到以下错误:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.panasonic.persistence.entity.LocationSettingEntity.
    at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
    at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
    at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:210)
    at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:220)
    at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:196)
    at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:128)
    at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:61)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
    at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
    at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:84)
    at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:42)
    at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:129)
    at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160)
    at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:212)
    at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:101)
    at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:64)
    at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37)
    at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
    at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
    at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:140)
    at com.mongodb.operation.MixedBulkWriteOperation$Run$3.executeWriteCommandProtocol(MixedBulkWriteOperation.java:480)
    at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:646)
    at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:399)
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:179)
    at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:168)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:230)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168)
    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74)
    at com.mongodb.Mongo.execute(Mongo.java:781)
    at com.mongodb.Mongo$2.execute(Mongo.java:764)
    at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:2195)
    at com.mongodb.BulkWriteOperation.execute(BulkWriteOperation.java:136)
    at org.springframework.data.mongodb.core.DefaultBulkOperations.execute(DefaultBulkOperations.java:266)

我尝试调试并发现com.mongodb库检查数据类型的编解码器,当它找不到时,它会抛出错误。据我所知,spring mongo操作能够处理POJO。

我不明白我错过了什么。请帮忙 ! ! !

1 个答案:

答案 0 :(得分:0)

从技术上讲,您可以编写自定义编解码器here's how。 我这样做但是它没有用,然后我潜入了Mongo驱动程序代码,似乎有一个错误。

看起来他们在这里犯了一个错误 - com.mongodb.DBCollection#DBCollection(java.lang.String,com.mongodb.DB,com.mongodb.operation.OperationExecutor)

DBObjectCodec getDefaultDBObjectCodec() {
    return new DBObjectCodec(MongoClient.getDefaultCodecRegistry(),
                             DBObjectCodec.getDefaultBsonTypeClassMap(),
                             getObjectFactory());
}

在这里,他们从默认的静态编解码器注册表设置集合Codec,因此永远无法获取人们编写的自定义编解码器。互联网上有很多人坚持这个问题。

然而,可以通过将更新有效负载转换为DBObject来解决问题,这可以使用com.mongodb.BasicDBObjectBuilder类完成,并且非常简单。不确定性能是否与编解码器一样好。我正在考虑编写自己的DBObject构建器,它将为我提供性能。因为我认为基本的是使用json。

Update update = new Update();
update.pushAll("location", list.stream().map(BasicDBObjectBuilder.start().add(//key,value pairs).get()).collect(Collectors.toList()).toArray());
bulkOps = bulkOps.updateOne(query, update);
bulkOps.execute();