The $substrCP operator extracts part of a string using Unicode code-point positions. It is the recommended way to slice text when emoji, accents, and international characters must count as single characters.
01
Code-Point Slice
Character-safe.
02
Syntax
[ str, start, len ].
03
Emoji Safe
1 emoji = 1 unit.
04
$project Stage
Preview fields.
05
Use Cases
Limits, previews.
06
vs $substrBytes
Chars vs bytes.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $substrCP operator returns a substring starting at a zero-based code-point index for a given code-point length. CP stands for code points — the Unicode units that roughly match visible characters. For example, { $substrCP: [ "hello", 0, 3 ] } returns "hel".
Use $substrCP for tweet-style character limits, title previews, and any user-facing truncation. Use $substrBytes when you need storage-byte positions instead. Pair with $strLenCP to measure length before slicing.
💡
Beginner Tip
On "Hi 👋", { $substrCP: [ "Hi 👋", 0, 4 ] } returns the full string (4 code points). Byte-based slicing might break the emoji in the middle.
Foundation
📝 Syntax
The $substrCP operator takes a string, code-point start index, and code-point length:
Compute start as total code-point length minus 4, then slice 4 code points. Works correctly even when the username contains emoji or accented letters.
Applications
🚀 Use Cases
Display previews — show the first N characters of posts, comments, or descriptions in lists.
Character limits — enforce tweet-style or form-field max character counts.
International content — slice text with accents, CJK characters, and emoji without corruption.
Data cleanup — extract fixed segments from user-generated text for reporting.
🧠 How $substrCP Works
1
MongoDB reads the string
The expression resolves to a UTF-8 string from a field path or literal.
Input
2
Code-point start is applied
MongoDB moves to the zero-based code-point position. Each visible character (including most emoji) is one unit.
Position
3
Code points are extracted
Up to length complete code points are copied into the result.
Extract
=
✂
Character-safe substring
Use in $project for previews, limits, and Unicode-aware parsing.
Wrap Up
Conclusion
The $substrCP operator extracts substrings by Unicode code points, making it the best choice for user-facing character limits and previews. It handles emoji and international text correctly where byte-based operators may fail.
For storage-byte slicing, use $substrBytes instead. Next in the series: $subtract.
Use $substrCP for display truncation and character limits
Pair with $strLenCP to measure length and compute suffix slices
Prefer $substrCP over legacy $substr for clarity
Test with emoji and accented characters, not just ASCII
Guard null fields with $ifNull before slicing
❌ Don’t
Use $substrCP for index key byte limits — use $substrBytes
Assume it matches JavaScript .length for all Unicode (surrogate pairs differ)
Confuse $substrCP with $slice (arrays vs strings)
Use negative start indices — they are not supported
Forget that start beyond length returns an empty string
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $substrCP
Use these points when slicing strings in MongoDB.
5
Core concepts
✂01
Code Points
Char-safe slice.
Purpose
📝02
[ s, start, len ]
CP units.
Syntax
😀03
Emoji safe
No mid-char cut.
Unicode
🛠04
Previews
First N chars.
Use case
⚠05
vs $substrBytes
Display vs storage.
Compare
❓ Frequently Asked Questions
$substrCP returns part of a string starting at a zero-based code-point index for a given code-point length. Code points correspond to visible characters, so one emoji usually counts as one unit. It is the recommended operator for Unicode-safe substring extraction.
The syntax is { $substrCP: [ <string>, <start>, <length> ] }. Start is the zero-based code-point index; length is how many code points to include.
$substrCP slices by Unicode code points (characters users see). $substrBytes slices by UTF-8 storage bytes. Use $substrCP for display truncation; use $substrBytes for storage or index byte limits.
Always prefer $substrCP over the legacy $substr operator when you care about correct character boundaries with emoji, accents, or international text. $substrCP is explicit and Unicode-aware.
If the string expression is null, $substrCP returns null. Use $ifNull to provide a default empty string when the field may be missing.