MongoDb中的文档大小 [英] Document Size in MongoDb

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

问题描述

将来想切换到DynamoDB,但要确保我的文档在1KB以下,因为它们每KB收费.有没有一种快速的方法来知道集合中文档的大小?

解决方案

作为一般指南,您可以使用集合报告的avgObjSize值检查集合 collname 中文档的平均大小. c1>:

db.collname.stats()

要查找和计算大型文档,您可以使用类似于以下内容的东西:

var maxSize = 1024;
var bigDocs = 0;
db.collname.find().forEach(
    function (doc) {
        var docSize = Object.bsonsize(doc);
        if (docSize >= maxSize) {
            bigDocs++;
            print(doc._id + ' is ' + docSize + ' bytes');
        }
    }
)
print("Found " + bigDocs + " documents bigger than " + maxSize + " bytes")

请注意,这两个示例都使用MongoDB BSON 表示形式,该表示形式与表示其他数据库中相同数据所需的大小有所不同. /p>

Thinking of switching to DynamoDB in future, but want to make sure my documents are under the 1KB, as they charge per KB. Is there a quick way to know how large a document is in a collection?

解决方案

As a general guide you can check the average size of documents in a collection collname using the avgObjSize value reported by collection stats():

db.collname.stats()

To find and count large documents you can use something similar to:

var maxSize = 1024;
var bigDocs = 0;
db.collname.find().forEach(
    function (doc) {
        var docSize = Object.bsonsize(doc);
        if (docSize >= maxSize) {
            bigDocs++;
            print(doc._id + ' is ' + docSize + ' bytes');
        }
    }
)
print("Found " + bigDocs + " documents bigger than " + maxSize + " bytes")

Note that both these examples are using the MongoDB BSON representation, which will vary from the size required to represent the same data in other databases.

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

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