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

Viewed 481

The following error occurred while reading a POJO, be able to insert,update and delete successfully.

The type of audid is string, most of reads are normal

mongo-java-driver-3.8.2.jar

Part of the code:

public class TestData implements Serializable {

  private String audid;

}

The error occurs in findIterable.iterator().

FindIterable<TestData> findIterable = testCollection.find(filter, TestData.class)
            .sort(sort)
            .batchSize(1000);
    if (0 != limit) {
        findIterable.limit(limit);
    }
MongoCursor<TestData> dbCursor = findIterable.iterator();

error logs:

org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'TestData' failed with the following exception:

Failed to decode 'TestData'. Decoding 'audid' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is INT64.

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)
    at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
    at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
    at org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47)
    at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101)
    at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
    at com.mongodb.internal.connection.ReplyMessage.<init>(ReplyMessage.java:48)
    ...
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'TestData'. Decoding 'audid' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is INT64.
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:195)
    at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:173)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:127)
    at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:131)
    at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37)
    ... 45 common frames omitted
Caused by: org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is INT64.
    at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:690)
    at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:722)
    at org.bson.AbstractBsonReader.readString(AbstractBsonReader.java:457)
    at org.bson.codecs.StringCodec.decode(StringCodec.java:39)
    at org.bson.codecs.StringCodec.decode(StringCodec.java:28)
    at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93)
    at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:189)
    ... 49 common frames omitted
1 Answers

Please use the below code in your TestData class.

    @BsonId
    private String audid;

    public String getAudid(){
        return audid;
    }
    public void setAudid(String audid){
        this.audid = audid;
    }

Also ensure you pass audid as String i.e., within double quotes(" ") while inserting data.

Related