解决MongoDB读数据报异常readEndDocument can only be called when State is END_OF_DOCUMENT

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)
    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;
    }

你可能感兴趣的:(mongodb)