在MongoDB中查找最大的文档大小 [英] Find largest document size in MongoDB

查看:370
本文介绍了在MongoDB中查找最大的文档大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在MongoDB中找到最大的文档大小?

Is it possible to find the largest document size in MongoDB?

db.collection.stats()显示的是平均大小,这并不是真正的代表,因为在我的情况下,大小可能相差很大.

db.collection.stats() shows average size, which is not really representative because in my case sizes can differ considerably.

推荐答案

您可以使用一个小的Shell脚本来获取该值.

You can use a small shell script to get this value.

注意:这将执行全表扫描,对于大型馆藏来说这会很慢.

Note: this will perform a full table scan, which will be slow on large collections.

let max = 0, id = null;
db.test.find().forEach(doc => {
    const size = Object.bsonsize(doc); 
    if(size > max) {
        max = size;
        id = doc._id;
    } 
});
print(id, max);

这篇关于在MongoDB中查找最大的文档大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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