bson的size()函数返回的int值是字节数吗?(无法找到此API的详细信息)。如何获取bson文档的字节大小?这是我的密码。
import org.bson.Document;
MongoDatabase db...;
MongoCollection<Document> mongoCollection = db.getCollection(...);
MongoCursor<Document> cursor = mongoCollection.find().iterator();
Document doc = cursor.next();
int size = doc.size(); // does this return number of bytes
// how to get size of doc in bytes?
最佳答案
将文档作为RawBsonDocument
获取,然后使用getByteBuffer().remaining()
方法。
MongoCollection<RawBsonDocument> mongoCollection = db.getCollection(collectionName, RawBsonDocument.class);
MongoCursor<RawBsonDocument> cursor = mongoCollection.find().iterator();
RawBsonDocument doc = cursor.next();
int size = doc.getByteBuffer().remaining()