当前位置:   article > 正文

解决MongoDB读数据报异常readEndDocument can only be called when State is END_OF_DOCUMENT_org.bson.bsoninvalidoperationexception: readstartd

org.bson.bsoninvalidoperationexception: readstartdocument can only be called
org.bson.BsonInvalidOperationException: readEndDocument can only be called when State is END_OF_DOCUMENT, not when State is NAME.
    at org.bson.AbstractBsonReader.throwInvalidState(AbstractBsonReader.java:671)
    at org.bson.AbstractBsonReader.readEndDocument(AbstractBsonReader.java:357)
    at com.jianmi.cloud.mongo.content.SmsContentCodec.decode(SmsContentCodec.java:42)
    at com.jianmi.cloud.mongo.content.SmsContentCodec.decode(SmsContentCodec.java:16)
    at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52)
    at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:53)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
    at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
    public SmsContentEntity decode(BsonReader reader, DecoderContext decoderContext) {
        SmsContentEntity user = new SmsContentEntity();
        reader.readStartDocument();
        user.setId(reader.readObjectId("_id").toString());
        user.setAccount(reader.readString("account"));    //用户名 
        user.setContent(reader.readString("content"));    //内容 


        //因为多了一些字段不读取,这里都跳过一下
        while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            System.out.println("reader.readName:"+reader.readName());
            reader.skipValue();
        }

        reader.readEndDocument();
        return user;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/140786
推荐阅读
  

闽ICP备14008679号