$bsonSize is a MongoDB aggregation expression that returns the size in bytes of a document or object when encoded as BSON. It counts field names, type metadata, and values together.
Remember
$bsonSize = BSON-encoded size of an object
{ $bsonSize: <object expression> }
input → object or null
output → integer bytes (or null)
$$ROOT → whole current document
"$meta" → one subdocument field
null → null
string / array / number → pipeline error
for each document:
obj = evaluate(expression) // often $$ROOT
if obj is null or missing:
result = null
else if obj is object:
result = bson_encode(obj).length // bytes
else:
error // aggregation fails
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while measuring document sizes.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $bsonSize: <object> }
Accepted
Object (document/subdocument) or null
Whole doc
{ $bsonSize: "$$ROOT" }
Subdocument
{ $bsonSize: "$meta" }
Limit reminder
MongoDB documents max out at 16 MB
Sample check
{ _id: 1, name: "Ada" } → 28 bytes
Try it
Live Preview
Watch $bsonSize grow as a document gains fields. Each extra key adds name bytes and type metadata, not just the value.
Simulates { $bsonSize: "$$ROOT" } on the sample profiles below.
Worked Walkthrough — Why BSON Size Is Bigger Than Text
The string "Ada" is only 3 UTF-8 bytes, but the whole document { _id: 1, name: "Ada" } is 28 bytes in BSON.
Calculation
Document:
{ _id: 1, name: "Ada" }
BSON pays for more than the word "Ada":
• document length prefix
• type byte for each field
• field names: "_id" and "name"
• value storage (int32 + string length + text + nulls)
• trailing document null
$bsonSize("$$ROOT") → 28
Compare:
$binarySize("$name") → 3 (content only)
$bsonSize("$$ROOT") → 28 (whole document)
Code
MongoDB Examples
We store three small profiles, measure each document with $$ROOT, then find the largest one. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine a tiny profiles collection. Each document has a little more data than the one before it.
Easy check. Alan has the most fields, so 64 bytes is the expected winner.
Edge Cases & Pitfalls
Check these before using $bsonSize in production pipelines.
types
Objects only
Strings, arrays, and numbers make the pipeline fail. Use $binarySize for string/BinData fields, or guard with $type.
null
Null subdocuments return null
Missing or null objects return null, not 0. Handle them before $sum or $sort if nulls are invalid.
overhead
BSON size > content size
Field names and type bytes count. A short string inside a document is always smaller than the whole document’s $bsonSize.
limit
Stay under 16 MB
MongoDB’s document size limit is 16 MB. Use $bsonSize to find docs approaching that ceiling before writes fail.
Analysis
Performance Notes
Approach
Cost
Best for
$bsonSize: "$$ROOT"
Per-document encode/measure
Document size audits
+ $sort / $limit
O(docs) + sort
Finding the largest documents
+ $group / $sum
O(docs)
Total collection BSON footprint
Measuring size is still cheaper than large joins. Filter early with $match when only some documents need a size check.
Remember
Key Takeaways
Role: BSON size of a document or object → bytes.
Whole doc: use "$$ROOT".
Inputs: object or null only.
Not field content: use $binarySize for one string/BinData field.
One line:$bsonSize answers “how many bytes is this document as BSON?”—including names and metadata, not just the text values.
Frequently Asked Questions
$bsonSize returns the size in bytes of a document or object when encoded as BSON. That includes field names, type bytes, and values—not just the raw string or binary content.
$$ROOT is the whole document currently moving through the pipeline. { $bsonSize: "$$ROOT" } measures that full document’s BSON size.
The argument must resolve to an object (document/subdocument) or null. Strings, arrays, numbers, and other BSON types cause the aggregation to fail with an error.
$bsonSize measures an entire BSON-encoded object. $binarySize measures only one string or BinData field’s content in bytes.
$bsonSize returns null when the input is null or the field is missing. That is useful for optional subdocuments.
It helps find oversized documents, stay aware of MongoDB’s 16 MB document limit, compare schema designs, and sum storage across a collection.