如何查看文档的大小?常见是查看集合中平均文档大小,但很少查看单个文档或者特定范围文档的大小甚至查看文档中字段长度大小?通过查看官方文档来解答如上问题,默认返回都是字节为单位(byte),以下整理自官方文档以及jira.
1、查看集合中文档的平均大小
mongos> db.tms_province_agg_result.stats().avgObjSize;
304
2、查看集合中单个文档或者单个文档大小,只能查看单个文档
Object.bsonsize(<document>)
2.1 统计集合满足条件的单条文档的大小
--find返回cursor而不是bson文档
mongos> Object.bsonsize(db.tms_province_agg_result.find())
79372 --这个值是错误
--findOne返回文档--这个是正确用法
mongos> Object.bsonsize(db.tms_province_agg_result.findOne())
303
2.2 统计普通文档或者bson文档长度
mongos> Object.bsonsize([])
5
--统计整数长度有16,正常情况下整数长度是7,但整数在javascript里面作为double类型,所以需要额外11长度来空间
mongos> Object.bsonsize([1])
16
mongos>
--日期类型只有5
mongos> Object.bsonsize(new Date())
5
3、查看批量文档大小(mongo 4.4版本开始支持)
3.1 构造数据
代码语言:javascript复制db.employees.insertMany([
{
"_id": 1,
"name": "Alice", "email": "alice@company.com", "position": "Software Developer",
"current_task": {
"project_id": 1,
"project_name": "Aggregation Improvements",
"project_duration": 5,
"hours": 20
}
},
{
"_id": 2,
"name": "Bob", "email": "bob@company.com", "position": "Sales",
"current_task": {
"project_id": 2,
"project_name": "Write Blog Posts",
"project_duration": 2,
"hours": 10,
"notes": "Progress is slow. Waiting for feedback."
}
},
{
"_id": 3,
"name": "Charlie", "email": "charlie@company.com", "position": "HR (On Leave)",
"current_task": null
},
{
"_id": 4,
"name": "Dianne", "email": "diane@company.com", "position": "Web Designer",
"current_task": {
"project_id": 3,
"project_name": "Update Home Page",
"notes": "Need to scope this project."
}
}
]);
3.2 查看满足条件文档的长度(byte)
$$ROOT--表示当前行
{ $bsonSize: <object> } 通过aggregate
mongos>db.employees.aggregate([
//通过match匹配满足条件的记录
{"$match":{"_id":{"$gt":2}}},
//计算每一条文档的大小
{ $project: { name: "$name", object_size: { $bsonSize: "$$ROOT" } } }
])
{ "_id" : 3, "name" :"Charlie", "object_size" : 109 }
{ "_id" : 4, "name" : "Dianne","object_size" : 204 }
4、查看批量文档(mongo 4.4版本开始支持)中嵌套文档长度(byte)
db.employees.aggregate([
{ $project: { name:"$name", task_object_size: { $bsonSize: "$current_task" }} }
])
{ "_id" : 1, "name" : "Alice","task_object_size" : 109 }
{ "_id" : 2, "name" : "Bob","task_object_size" : 152 }
{ "_id" : 3, "name" :"Charlie", "task_object_size" : null }
{ "_id" : 4, "name" : "Dianne","task_object_size" : 99 }
5、查看文档中字符串、二进制的长度,需要通过$binarySize来实现
5.1 构造数据
代码语言:javascript复制db.images.insertMany([
{ _id: 1, name: "cat.jpg", binary: new BinData(0, "OEJTfmD8twzaj/LPKLIVkA==")},
{ _id: 2, name: "big_ben.jpg", binary: new BinData(0, "aGVsZmRqYWZqYmxhaGJsYXJnYWZkYXJlcTU1NDE1Z2FmZCBmZGFmZGE=")},
{ _id: 3, name: "tea_set.jpg", binary: new BinData(0, "MyIRAFVEd2aImaq7zN3u/w==")},
{ _id: 4, name: "concert.jpg", binary: new BinData(0, "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlzIHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2YgdGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGludWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRoZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=")},
{ _id: 5, name: "empty.jpg", binary: new BinData(0, "") }
])
5.2 查询集合中所有文档binary字段的长度(byte)
代码语言:javascript复制db.images.aggregate([
{
$project: {
"name": "$name",
"imageSize": { $binarySize: "$binary" }
}
}
])
{ "_id" : 1, "name" : "cat.jpg", "imageSize" : 16 }
{ "_id" : 2, "name" : "big_ben.jpg", "imageSize" : 41 }
{ "_id" : 3, "name" : "tea_set.jpg", "imageSize" : 16 }
{ "_id" : 4, "name" : "concert.jpg", "imageSize" : 269 }
{ "_id" : 5, "name" : "empty.jpg", "imageSize" : 0 }
5.3 通过match来过滤不需要的条件
代码语言:javascript复制db.images.aggregate([
//通过match匹配满足条件的记录
{"$match":{"_id":{"$gt":2}}},
//计算每一条文档中binary的大小
{ $project: { name: "$name", object_size: { $binarySize: "$binary" } } }
])
{ "_id" : 3, "name" : "tea_set.jpg", "object_size" : 16 }
{ "_id" : 4, "name" : "concert.jpg", "object_size" : 269 }
{ "_id" : 5, "name" : "empty.jpg", "object_size" : 0 }