如何获取bson文档的字节大小 [英] How to get size in bytes of bson documents

查看:274
本文介绍了如何获取bson文档的字节大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bson文档的size()函数返回的int值是字节数吗? (无法找到此API的详细信息).
如何获取bson文档的大小(以字节为单位)?这是我的代码.

Is the int value returned by size() function of bson document the number of bytes ? (not able to find the details of this API).
How can I get the size of a bson document in bytes? Here is my code.

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()方法.

Get the document as a RawBsonDocument then use the getByteBuffer().remaining() method.

MongoCollection<RawBsonDocument> mongoCollection = db.getCollection(collectionName, RawBsonDocument.class);
MongoCursor<RawBsonDocument> cursor = mongoCollection.find().iterator();
RawBsonDocument doc = cursor.next();
int size = doc.getByteBuffer().remaining()

这篇关于如何获取bson文档的字节大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆