如何获取bson文档的字节大小

时间:2015-12-31 11:08:31

标签: mongodb mongodb-java bson

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?

1 个答案:

答案 0 :(得分:0)

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()