Spring Boot已经有Jsr310Converters,但是BulkOperations存在问题。
这是一个模型:
@Document
@Data
@NoArgsConstructor
public class Data2 {
@Id
private String id;
private LocalDateTime createdAt;
private int value;
}
它适用于MongoRepository和MongoTemplate。这段代码工作正常:
Data2 data2 = new Data2();
data2.setValue(1);
data2.setCreatedAt(LocalDateTime.now());
data2Repository.save(data2);
mongoTemplate.updateFirst(
new Query(where("_id").is(data2.getId())),
new Update().set("createdAt", LocalDateTime.now().minusHours(1)).set("value", 2),
Data2.class);
但是BulkOperations失败了:
BulkOperations bulks = mongoTemplate.bulkOps(BulkOperations.BulkMode.UNORDERED, Data2.class);
bulks.updateOne(new Query(), new Update().set("createdAt", LocalDateTime.now().minusHours(1)).set("value", 3));
bulks.execute();
这是一个堆栈跟踪:
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class java.time.LocalDateTime.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46) ~[bson-3.2.2.jar:na]
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63) ~[bson-3.2.2.jar:na]
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37) ~[bson-3.2.2.jar:na]
at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:210) ~[mongodb-driver-3.2.2.jar:na]
at com.mongodb.DBObjectCodec.encodeMap(DBObjectCodec.java:220) ~[mongodb-driver-3.2.2.jar:na]
at com.mongodb.DBObjectCodec.writeValue(DBObjectCodec.java:196) ~[mongodb-driver-3.2.2.jar:na]
at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:128) ~[mongodb-driver-3.2.2.jar:na]
at com.mongodb.DBObjectCodec.encode(DBObjectCodec.java:61) ~[mongodb-driver-3.2.2.jar:na]
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63) ~[bson-3.2.2.jar:na]
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29) ~[bson-3.2.2.jar:na]
at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:84) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.UpdateCommandMessage.writeTheWrites(UpdateCommandMessage.java:42) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:129) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:212) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:101) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:64) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.UpdateCommandProtocol.execute(UpdateCommandProtocol.java:37) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.connection.DefaultServerConnection.updateCommand(DefaultServerConnection.java:140) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.operation.MixedBulkWriteOperation$Run$3.executeWriteCommandProtocol(MixedBulkWriteOperation.java:480) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:646) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:399) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:179) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:168) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:230) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74) ~[mongodb-driver-core-3.2.2.jar:na]
at com.mongodb.Mongo.execute(Mongo.java:781) ~[mongodb-driver-3.2.2.jar:na]
at com.mongodb.Mongo$2.execute(Mongo.java:764) ~[mongodb-driver-3.2.2.jar:na]
at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:2195) ~[mongodb-driver-3.2.2.jar:na]
at com.mongodb.DBCollection.executeBulkWriteOperation(DBCollection.java:2188) ~[mongodb-driver-3.2.2.jar:na]
at com.mongodb.BulkWriteOperation.execute(BulkWriteOperation.java:121) ~[mongodb-driver-3.2.2.jar:na]
at org.springframework.data.mongodb.core.DefaultBulkOperations.execute(DefaultBulkOperations.java:266) ~[spring-data-mongodb-1.9.3.RELEASE.jar:na]
at scratch4.service.DataService.test2(DataService.java:71) ~[classes/:na]
Spring Boot版本:1.4.1.RELEASE
答案 0 :(得分:1)
我认为问题是没有为java.time.LocalDateTime
注册转换器。
我解决它的方法是首先将其转换为DBObject
,然后从中创建更新对象:
Update.update(key, template.getConverter().convertToMongoType(value));
不确定这是否适用于您的情况。