gpt4 book ai didi

java - mongo-operations spring mongo批量操作执行异常(CodecConfigurationException : Can't find a codec for class)

转载 作者:可可西里 更新时间:2023-11-01 10:49:32 25 4
gpt4 key购买 nike

我尝试使用 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。

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

最佳答案

从技术上讲,您将编写自定义编解码器 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());
}

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

然而,这个问题可以通过将更新有效负载转换为 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();

关于java - mongo-operations spring mongo批量操作执行异常(CodecConfigurationException : Can't find a codec for class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43702704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com