Sample Input Documents
Suppose you have a messages collection with text fields of varying length:
[
{ "_id": 1, "text": "Hello" },
{ "_id": 2, "text": "MongoDB rocks!" },
{ "_id": 3, "text": "café" },
{ "_id": 4, "text": "Hi 👋" }
] 
The $strLenBytes operator returns how many bytes a string occupies in UTF-8 encoding. Use it for storage validation, index size checks, and any scenario where byte length matters more than visible character count.
UTF-8 storage size.
One string expression.
1 byte per ASCII char.
Add length fields.
Limits, validation.
Bytes vs code points.
In MongoDB’s aggregation framework, the $strLenBytes operator counts the number of bytes in a string. MongoDB stores strings as BSON UTF-8, so plain ASCII characters use one byte each, while accented letters, CJK characters, and emoji may use two, three, or four bytes per character.
For example, { $strLenBytes: "hello" } returns 5. A string with emoji can have fewer visible characters but more bytes. When you need character count instead, use $strLenCP (code points).
For English-only ASCII text, byte length equals character count. As soon as your data includes international text or emoji, byte length and character count can differ.
The $strLenBytes operator takes one string expression:
{ $strLenBytes: <string expression> } { $strLenBytes: "hello" }
// Result: 5 (5 ASCII bytes)
{ $strLenBytes: "café" }
// Result: 5 (é uses 2 bytes in UTF-8)
{ $strLenBytes: "$message" }
// Byte length of the message field $strLenBytes — returns an integer byte count.<string expression> — field path, literal, or nested string expression.$project, $addFields, $set, or $match with $expr.null input — returns null."Hi 👋" — ~7 bytes, ~4 code points| Question | Answer |
|---|---|
| Operator type | Aggregation expression operator (string) |
| Syntax | { $strLenBytes: <string> } |
| Output | Integer (byte count) or null |
| Encoding | UTF-8 (BSON default) |
| Related operator | $strLenCP for character count |
| Common stages | $project, $addFields, $match + $expr |
{
$strLenBytes: "hello"
}Returns 5
{
$strLenBytes: "$text"
}Field byte length
$gt: [
{ $strLenBytes: "$msg" },
280
]Over limit
{
$strLenBytes: null
}Returns null
Measure byte length on ASCII and UTF-8 strings, filter by size limits, and compare with character-based length.
Start with a messages collection and compute byte length with $project.
Suppose you have a messages collection with text fields of varying length:
[
{ "_id": 1, "text": "Hello" },
{ "_id": 2, "text": "MongoDB rocks!" },
{ "_id": 3, "text": "café" },
{ "_id": 4, "text": "Hi 👋" }
] Add a byteLength field to each document:
db.messages.aggregate([
{
$project: {
text: 1,
byteLength: { $strLenBytes: "$text" }
}
}
]) Each character’s UTF-8 encoding determines the byte count. ASCII letters use one byte; extended characters and emoji use more.
Filter by byte limits and compare byte length with code-point length.
Compare $strLenBytes with $strLenCP on the same string:
db.messages.aggregate([
{
$project: {
text: 1,
bytes: { $strLenBytes: "$text" },
codePoints: { $strLenCP: "$text" }
}
}
])
// "Hi 👋" → bytes: 7, codePoints: 4
// "Hello" → bytes: 5, codePoints: 5 When bytes exceed code points, the string contains multi-byte UTF-8 characters. This is normal for international text and emoji.
Find messages longer than 280 bytes (useful for storage or API limits):
db.messages.aggregate([
{
$match: {
$expr: {
$gt: [
{ $strLenBytes: "$text" },
280
]
}
}
},
{
$project: {
text: 1,
byteLength: { $strLenBytes: "$text" }
}
}
]) Wrap $strLenBytes in a comparison inside $expr. Only documents exceeding the byte threshold pass the $match stage.
Add a boolean field indicating whether text fits within a byte budget:
db.messages.aggregate([
{
$addFields: {
byteLength: { $strLenBytes: "$text" },
withinLimit: {
$lte: [
{ $strLenBytes: "$text" },
100
]
}
}
}
])
// text under 100 bytes → withinLimit: true
// text over 100 bytes → withinLimit: false Combine $strLenBytes with $lte or $gte to build validation flags without changing the original text field.
Edge cases for missing or empty values:
db.messages.aggregate([
{
$project: {
emptyLen: { $strLenBytes: "" },
nullLen: { $strLenBytes: null },
safeLen: {
$strLenBytes: {
$ifNull: [ "$text", "" ]
}
}
}
}
])
// "" → 0
// null → null
// $ifNull guards missing fields An empty string has zero bytes. Use $ifNull when the field may be missing so you get 0 instead of null.
$strLenBytes WorksThe expression resolves to a BSON UTF-8 string from a field path or literal.
Each character’s encoded bytes are summed. ASCII = 1 byte; extended Unicode may use more.
The total byte count is stored in the field you define in the pipeline stage.
Use for limits, validation, and comparison with $strLenCP.
The $strLenBytes operator measures string length in UTF-8 bytes inside aggregation pipelines. It is essential when storage size, index limits, or byte-based API constraints matter more than visible character count.
For user-facing character limits, pair your knowledge with $strLenCP. Next in the series: $strLenCP.
$strLenBytes for storage and index byte-limit checks$strLenCP when debugging UTF-8 length differences$ifNull: [ "$field", "" ]$gt, $lte in $expr for filtering$strLenCP$strLenBytes with JavaScript’s .length (UTF-16 code units)null input returns null$substrBytes instead)$strLenBytesUse these points when measuring strings in MongoDB.
UTF-8 storage.
PurposeOne argument.
SyntaxEmoji & accents.
EncodingFilter & validate.
Use caseBytes vs chars.
CompareMove on to $strLenCP for code-point character counts, or review $strcasecmp for case-insensitive comparison.
5 people found this page helpful