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?
1 Answer
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.
-
According to these docs (docs.mongodb.org/manual/reference/limits/#BSON Document Size),
The maximum BSON document size is 16 megabytes.
Commented Nov 1, 2013 at 17:41 -
3@KevinMeredith: You are correct that the maximum MongoDB document size is 16MB (as at MongoDB 2.4), however the question posed was how to check the actual size of documents and keep them under 1KB.– StennieCommented Nov 2, 2013 at 12:01