$strLenCP is a MongoDB aggregation expression that returns the length of a string in Unicode code points—how many characters the string has. Use it for user-facing limits. For storage bytes, use $strLenBytes.
Remember
$strLenCP = character count (code points) for aggregation
{ $strLenCP: <string expression> }
"hello" → 5
"café" → 4
"🙂" → 1 (often 4 bytes in UTF-8)
null / missing → pipeline error
need bytes? → use $strLenBytes
Ensure the argument is a string (use $ifNull if the field may be missing).
Pass that expression to $strLenCP.
Optionally compare with $strLenBytes when storage also matters.
Piece
Required?
Role
$strLenCP
Yes
Unicode code-point 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 = unicodeCodePointCount(s)
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while measuring character length.
Topic
Detail
Type
Aggregation expression (not a find filter)
Syntax
{ $strLenCP: <string> }
Counts
Unicode code points (characters)
vs $strLenBytes
Characters vs UTF-8 bytes
Null-safe pattern
{ $strLenCP: { $ifNull: ["$title", ""] } }
Empty string
$strLenCP("") → 0
Sample check
"café" → 4; "🙂" → 1
Try it
Live Preview
Watch $strLenCP count characters. Compare ASCII text with accents and emoji.
Simulates $strLenCP (and shows $strLenBytes for comparison).
The word café has 4 characters. Byte length is higher because é needs 2 UTF-8 bytes—but $strLenCP still reports 4.
Calculation
String: "café"
Code points:
c, a, f, é → 4 characters
$strLenCP("café") → 4
$strLenBytes("café") → 5
Check: user sees 4 letters; storage uses 5 bytes ✓
Code
MongoDB Examples
We store two short titles: plain ASCII and accented text. First we measure characters, then we compare with byte length. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine two titles: hello (ASCII) and café (accented). We save them in a collection named titles.
Some “single” emoji or accented glyphs are multiple code points combined. $strLenCP counts code points, not visual grapheme clusters.
Analysis
Performance Notes
Approach
Cost
Best for
$strLenCP alone
Per-document Unicode scan
Form limits, display validation
+ $strLenBytes
Two length passes
Teaching / dual constraints
$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 character-count check.
Remember
Key Takeaways
Role: Unicode code-point (character) length.
Use when: user-facing character limits matter.
Bytes: use $strLenBytes instead.
Nulls: error — default with $ifNull first.
One line:$strLenCP answers “how many Unicode characters does this string have?”—not how many UTF-8 bytes it stores.
Frequently Asked Questions
$strLenCP returns the length of a string in Unicode code points—how many characters the string has. For example, { $strLenCP: "hello" } returns 5, and { $strLenCP: "café" } returns 4. Use it inside aggregation expression stages such as $project and $addFields.
Use { $strLenCP: <string expression> }. The argument can be a field like "$title" or a literal string.
$strLenCP counts code points (characters). $strLenBytes counts UTF-8 storage bytes. One emoji is often 1 code point but 4 bytes, so the two values can differ.
Use $strLenCP for user-facing character limits—form fields, display validation, and any rule based on how many characters appear, not how many bytes are stored.
If the argument is null or the field is missing, $strLenCP fails with an error. Guard with $ifNull (for example default to "") before measuring.
Yes for simple Unicode. Spreading a string into code points (or Array.from) matches the idea better than string.length, which counts UTF-16 code units. Run $strLenCP in the aggregation pipeline, not as a standalone find() filter.