How to find the size of the largest document in all db's

Hi,
We have more than 100 dbs and I would like to know if there is a way to find what is the size of the max doc present here (1 max size doc and details of the document like object id, db name etc present in the whole of all db’s, not one max size for each db)
Regards,
Chitra

Welcome to the MongoDB Community @chitra_Lakshminarayanan !

Finding the maximum document size across your whole deployment will require iterating through every document. The logic to do so is straightforward, but the impact on your deployment’s working set could be significant as it would be a full document scan for every collection.

The general approach would be:

  • for each database
    • for each collection
      • find all documents and compare the BSON size of each document to the largest seen so far (or use the $bsonSize aggregation operator in MongoDB 4.4+).

To get you started, here is an example aggregation to find the size and _id of the largest document in a collection:

db.mycollection.aggregate([
	{ $addFields: {
		bsonsize: { $bsonSize: "$$ROOT" }
	}},
	{ $sort: { bsonsize: -1 }},
	{ $limit: 1 },
	{ $project: {
		_id: 1,
		bsonsize: 1
	}}
])

Regards,
Stennie

8 Likes