我收到以下错误-在读取POJO时,我能够成功插入/更新和删除。
我们正在使用mongodb-driver-reactivestreams(1.9.0)
org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'FooBar' failed with the following exception:
Failed to decode 'FooBar'. Decoding '_id' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:40) ~[bson-3.7.1.jar:?]
at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52) ~[mongodb-driver-core-3.8.0.jar:?]
at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60) ~[mongodb-driver-core-3.8.0.jar:?]
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84) ~[bson-3.7.1.jar:?]
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41) ~[bson-3.7.1.jar:?]
at org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47) ~[bson-3.7.1.jar:?]
at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101) ~[bson-3.7.1.jar:?]
at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63) ~[mongodb-driver-core-3.8.0.jar:?]
at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84) ~[bson-3.7.1.jar:?]
at
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'FooBar'. Decoding '_id' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:195) ~[bson-3.7.1.jar:?]
at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:173) ~[bson-3.7.1.jar:?]
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:127) ~[bson-3.7.1.jar:?]
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:131) ~[bson-3.7.1.jar:?]
at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37) ~[bson-3.7.1.jar:?]
... 46 more
Caused by: org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.
at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:690) ~[bson-3.7.1.jar:?]
at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:722) ~[bson-3.7.1.jar:?]
at org.bson.AbstractBsonReader.readString(AbstractBsonReader.java:457) ~[bson-3.7.1.jar:?]
at org.bson.codecs.StringCodec.decode(StringCodec.java:39) ~[bson-3.7.1.jar:?]
at org.bson.codecs.StringCodec.decode(StringCodec.java:28) ~[bson-3.7.1.jar:?]
at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93) ~[bson-3.7.1.jar:?]
at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:189) ~[bson-3.7.1.jar:?]
at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:173) ~[bson-3.7.1.jar:?]
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:127) ~[bson-3.7.1.jar:?]
at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:131) ~[bson-3.7.1.jar:?]
at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37) ~[bson-3.7.1.jar:?]
... 46 more
答案 0 :(得分:0)
只需将注释@BsonProperty("id")
添加到“ id”字段即可,
答案 1 :(得分:0)
需要两个更改才能解决此问题。
在字段ID上添加以下注释:- @BsonProperty(“ _ id”) @BsonId
使字段ObjectId代替String:- ObjectId ID;
现在为什么能解决这个问题。
因为如果在mongo shell中看到id的数据类型是这样的:-
“ _ id”:ObjectId(“ 5d7780d141962c0628915748”) 此处的“ _id”字段为ObjectId类型。我们的错误消息是
“解码'_id'错误:readString只能在CurrentBSONType为STRING时调用,而在CurrentBSONType为OBJECT_ID时不能调用。”在读取编解码器转换器时尝试将_id转换为String,并且此转换没有自动代码(从ObjectId到String),因此我们收到“无法解码...”错误。 因此,如果我们将DataType从String更改为ObjectId,则它是直接映射并解决了问题。
在插入的情况下,无论我们要插入的数据是什么,只要将它们插入具有相同文件类型的mongo中,就无需转换。 如果是“更新和删除”,它会尝试使用字符串查找(如果找不到),然后也不会出现“无法解码'_id'”错误。
答案 2 :(得分:0)
只需在我的pojo的“ id”属性中添加@BsonId注释就可以了。不仅“错误解码'_id'”错误消失了,它还将我的pojo的“ id”属性映射到bson文档中的“ _id”字段,反之亦然-到目前为止,我有两个标识符“ id”和“ _id”。