readString can only be called when CurrentBSONType is STRING, not when CurrentBS

4,165 views
Skip to first unread message

Durga Durga

unread,
Aug 16, 2018, 11:50:28 PM8/16/18
to mongodb-user

I get the following error - while reading a POJO, I am able to insert / update and delete successfully.

We are using 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

Jeff Yemin

unread,
Aug 17, 2018, 7:56:39 AM8/17/18
to mongod...@googlegroups.com
Hi,

It looks like the type of the _id field in the database is ObjectId, but the type of the _id field in the FooBar class is String.  They need to be the same.  Try changing the type of the field in FooBar to ObjectId.  If that doesn't work, please post the full class definition of FooBar as well as the JSON representation of the document that you're trying to decode.


Regards,
Jeff


Durga Durga

unread,
Aug 20, 2018, 3:38:46 PM8/20/18
to mongodb-user
Hi Jeff,

  My POJO doesn't have an _id field

  The above error comes - when I try to list out all the POJOS in my collection.

HTH
_
Reply all
Reply to author
Forward
0 new messages