The $bsonSize operator returns how many bytes a document or object occupies when encoded as BSON. Use $$ROOT to measure the entire current document, or pass a subdocument field to audit nested data.
01
BSON Bytes
Full encoded size.
02
Syntax
One object expression.
03
$$ROOT
Measure whole document.
04
MongoDB 4.4+
Aggregation operator.
05
Use Cases
Storage, limits, tuning.
06
vs $binarySize
Document vs one field.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $bsonSize operator calculates the BSON-encoded size of an object in bytes. This includes field names, BSON type markers, and values — not just the raw data payload. A small document with long field names can be larger than you expect.
💡
Beginner Tip
MongoDB enforces a 16 MB maximum document size. Use { $bsonSize: "$$ROOT" } to check how close documents are to that limit before inserts or schema changes.
Foundation
📝 Syntax
The $bsonSize operator takes one object expression:
mongosh
{ $bsonSize: <object> }
Syntax Rules
$bsonSize — returns the BSON-encoded size in bytes.
<object> — must resolve to an object (document) or null.
Use $$ROOT to reference the entire document in the current pipeline stage.
Use a field path like "$metadata" to measure a nested subdocument.
$$ROOT refers to the whole document at the current pipeline stage. $bsonSize serializes it as BSON and counts every byte, including _id, field names, and array structure.
📈 Practical Patterns
Measure subdocuments, find outliers, and aggregate total collection size.
Example 2 — Measure a Subdocument Field
Check how much space a nested object consumes without counting the rest of the document:
Pass any object field to $bsonSize. If the field is missing or null, the result is null. This is useful for comparing which nested structures grow fastest.
Example 3 — Find the Largest Documents
Add a size field, sort descending, and return the top offenders:
Schema comparison — measure BSON size before and after renaming fields or restructuring data.
Capacity planning — sum $bsonSize across collections for growth estimates.
🧠 How $bsonSize Works
1
MongoDB reads the object
The pipeline evaluates the expression — typically "$$ROOT" or a subdocument field like "$metadata".
Input
2
$bsonSize encodes as BSON
MongoDB serializes the object into BSON format and counts every byte, including type markers and field names.
Encode
3
The byte count is stored in the pipeline
The integer result is written to the field you define in $project or $addFields.
Output
=
🗃
Size data for optimization
Sort, filter, sum, or alert on document sizes in later pipeline stages.
Wrap Up
Conclusion
The $bsonSize operator is essential for understanding how much space your MongoDB documents actually consume on disk. Combined with $$ROOT, it gives you a precise byte count for storage audits and schema tuning.
For beginners, remember: it measures BSON-encoded objects (not single string fields), use $$ROOT for the full document, and pair it with $sort to find outliers.
Monitor documents approaching the 16 MB BSON limit
Compare subdocument sizes to find bloated nested fields
Use allowDiskUse: true when sorting large collections by size
Pair with $binarySize when you need field-level detail
❌ Don’t
Confuse $bsonSize with $binarySize
Expect character count — BSON size includes metadata overhead
Use on MongoDB versions before 4.4
Pass strings or numbers directly (object or null only)
Forget that short field names reduce BSON overhead
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $bsonSize
Use these points when measuring document storage in MongoDB.
5
Core concepts
🗃01
BSON Bytes
Full encoded object size.
Purpose
📝02
Simple Syntax
{ $bsonSize: obj }
Syntax
📏03
$$ROOT
Whole document reference.
Variable
🛠04
16 MB Limit
Monitor large docs.
Limit
⚠05
Not $binarySize
Document vs one field.
Important
❓ Frequently Asked Questions
$bsonSize returns the size in bytes of a document or object when encoded as BSON. It includes field names, types, and values — not just raw content.
The syntax is { $bsonSize: <object> }. Use $$ROOT to measure the entire current document, or pass a subdocument field like "$metadata".
$$ROOT is a system variable that refers to the whole document currently being processed in the pipeline. { $bsonSize: "$$ROOT" } measures the full document size.
$bsonSize measures an entire BSON-encoded object (document or subdocument). $binarySize measures only a single string or BinData field's content in bytes.
It helps find large documents, monitor storage, stay under MongoDB's 16 MB document limit, and compare schema designs before and after changes.