The $binarySize operator returns the byte size of a string or BinData value in MongoDB aggregation pipelines. Use it to audit attachments, track storage, or find the largest binary fields.
01
Byte Size
Measure content in bytes.
02
Syntax
One expression inside $binarySize.
03
Strings & BinData
Works on both types.
04
MongoDB 4.4+
Aggregation expression operator.
05
Use Cases
Files, storage, auditing.
06
vs $bsonSize
Field vs whole document.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $binarySize operator returns how many bytes a string or BinData value occupies. For example, the string "Hello" is 5 bytes, and BinData(0, "SGVsbG8=") (base64 for "Hello") is also 5 bytes.
💡
Beginner Tip
$binarySize measures one field’s content. To measure an entire document’s BSON footprint, use $bsonSize instead. They answer different questions.
Foundation
📝 Syntax
The $binarySize operator takes one expression:
mongosh
{ $binarySize: <expression> }
Syntax Rules
$binarySize — returns the size in bytes of the expression value.
<expression> — must resolve to a string, BinData, or null.
Use it inside stages like $project, $addFields, or $set.
If the input is null, the result is null.
Arrays, numbers, and other BSON types cause an aggregation error.
Available in MongoDB 4.4 and later.
⚠️ Supported Input Types
$binarySize only accepts string and BinData values:
$binarySize: "$fileData" → bytes(BinData field)
$binarySize: "$title" → bytes(string field)
$binarySize: "$tags" → error(array not supported)
$binarySize: null → null(safe)
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (data size)
Syntax
{ $binarySize: <expression> }
Accepted types
string, BinData, null
Output unit
Bytes (integer)
MongoDB version
4.4+
BinData
{
$binarySize: "$data"
}
File attachment bytes
String
{
$binarySize: "$title"
}
Text field byte length
Literal
{
$binarySize: "Hello"
}
Returns 5
Null input
{
$binarySize: null
}
Returns null
Hands-On
Examples Gallery
Measure binary and string field sizes, find the largest attachment, and sum total bytes across a collection.
📚 Measure Field Size
Use an images collection with BinData file fields and compute byte sizes with $project.
Sample Input Documents
Suppose you have an images collection storing uploaded files as BinData:
Project the byte size per document, then use $group with $sum and $avg for collection-level storage statistics. Documents with a null binary field contribute 0 to the sum.
Applications
🚀 Use Cases
Storage auditing — measure how much space binary attachments consume in a collection.
File upload tracking — record and report byte sizes of uploaded images, PDFs, or encoded payloads.
Capacity planning — sum binary sizes to estimate growth and set storage alerts.
Data quality checks — flag documents with unexpectedly large or empty binary fields.
🧠 How $binarySize Works
1
MongoDB reads the field value
The pipeline evaluates the expression — typically a field like "$binary" or "$title".
Input
2
$binarySize counts the bytes
MongoDB measures the raw content length of the string or BinData value. Unsupported types trigger an error.
Measure
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 ready for analysis
Sort, filter, sum, or average the byte counts in later pipeline stages.
Wrap Up
Conclusion
The $binarySize operator is a practical tool for measuring string and BinData field sizes in MongoDB aggregation pipelines. It answers a simple question: how many bytes does this field contain?
For beginners, remember: it works on strings and BinData only, returns bytes as an integer, and is different from $bsonSize which measures entire documents.
Use $binarySize for individual string or BinData fields
Combine with $sort to find the largest attachments
Use $sum in $group for total storage reports
Handle null fields gracefully (they return null, not an error)
Use $bsonSize when you need whole-document size
❌ Don’t
Pass arrays, numbers, or objects to $binarySize
Confuse $binarySize with $bsonSize
Assume the count includes BSON type metadata overhead
Use on MongoDB versions before 4.4
Expect character count for strings — it returns byte length
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $binarySize
Use these points when measuring data sizes in MongoDB.
5
Core concepts
🗃01
Byte Size
Counts content in bytes.
Purpose
📝02
Simple Syntax
{ $binarySize: expr }
Syntax
📏03
String & BinData
Two supported types.
Input
🛠04
MongoDB 4.4+
Aggregation operator.
Version
⚠05
Not $bsonSize
Field vs document.
Important
❓ Frequently Asked Questions
$binarySize returns the size in bytes of a string or BinData value. It is useful for measuring file attachments, encoded payloads, or text field lengths at the byte level.
The syntax is { $binarySize: <expression> }. The expression must resolve to a string, BinData, or null.
Yes. $binarySize accepts both strings and BinData values. For strings, it returns the byte length of the string content.
$binarySize measures one string or BinData field in bytes. $bsonSize measures the total BSON-encoded size of an entire document or object.
If the input is null, $binarySize returns null. If the input is an array, number, or other unsupported BSON type, the aggregation returns an error.