$strLenBytes is a MongoDB aggregation expression that returns the UTF-8 byte length of a string. ASCII letters are 1 byte each. Accents and emoji often need more bytes than the number of characters you see.
Ensure the argument is a string (use $ifNull if the field may be missing).
Pass that expression to $strLenBytes.
Optionally compare with $strLenCP to see bytes vs characters.
Piece
Required?
Role
$strLenBytes
Yes
UTF-8 byte length
String expression
Yes
Field path or literal string
$ifNull
No
Default to "" so missing fields do not error
Pseudocode
Pseudocode
for each document:
s = evaluate(stringExpression)
if s is null or missing:
error // aggregation fails
else if s is not a string:
error
else:
result = utf8ByteLength(s)
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while measuring string byte length.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $strLenBytes: <string> }
Counts
UTF-8 bytes (1–4 per character)
vs $strLenCP
Bytes vs Unicode code points
Null-safe pattern
{ $strLenBytes: { $ifNull: ["$msg", ""] } }
Empty string
$strLenBytes("") → 0
Sample check
"café" → 5 bytes; code points → 4
Try it
Live Preview
Watch $strLenBytes count UTF-8 bytes. Compare ASCII text with accents and emoji.
Simulates $strLenBytes (and shows $strLenCP for comparison).
The word café looks like 4 letters, but UTF-8 stores é with 2 bytes.
Calculation
String: "café"
UTF-8 bytes:
c → 1
a → 1
f → 1
é → 2
--------
total = 5
$strLenBytes("café") → 5
$strLenCP("café") → 4
Check: characters 4, storage bytes 5 ✓
Code
MongoDB Examples
We store two short messages: plain ASCII and accented text. First we measure bytes, then we compare bytes with code points. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine two messages: hello (ASCII) and café (accented). We save them in a collection named messages.
For plain English A–Z text, bytes and code points match. The difference appears with international text.
Analysis
Performance Notes
Approach
Cost
Best for
$strLenBytes alone
Per-document UTF-8 scan
Short fields, byte budgets
+ $strLenCP
Two length passes
Teaching / validating UTF-8 gaps
$match then length
Fewer docs to measure
Only some rows need a check
Length checks are cheap for short strings. Filter early with $match when only some documents need a byte-size check.
Remember
Key Takeaways
Role: UTF-8 byte length of a string.
Use when: storage, indexes, or payload size matter.
Characters: use $strLenCP instead.
Nulls: error — default with $ifNull first.
One line:$strLenBytes answers “how many UTF-8 bytes does this string use?”—not how many letters you see.
Frequently Asked Questions
$strLenBytes returns how many UTF-8 bytes a string uses. ASCII letters count as 1 byte each. Accents and emoji often use 2–4 bytes per character. Use it inside aggregation expression stages such as $project and $addFields.
Use { $strLenBytes: <string expression> }. The argument can be a field like "$message" or a literal string.
$strLenBytes counts bytes (storage size in UTF-8). $strLenCP counts Unicode code points (character count). One emoji may be 1 code point but 4 bytes.
Use $strLenBytes for storage limits, index key size, or payload byte budgets. Use $strLenCP when you care about how many characters a user sees.
If the argument is null or the field is missing, $strLenBytes fails with an error. Guard with $ifNull (for example default to "") before measuring.
Yes for UTF-8 strings. Think of $strLenBytes as MongoDB’s aggregation version of counting UTF-8 bytes—run it in the pipeline, not as a standalone find() filter.