$substrCP
The $substrCP operator returns a substring from a string, measured in UTF-8 code points (characters), starting at a specified character index with a specified number of characters.
๐ Syntax
{ "$substrCP": [ <string>, <startChar>, <charLength> ] }
<string>: The input string<startChar>: Character index to start from (0-based)<charLength>: Number of characters (code points) to return
โ Base Example 1 โ Extract First 4 Characters
๐ฅ Input Document
{ "title": "Notebook" }
๐ Expression
{ "$substrCP": ["$title", 0, 4] }
๐ค Output
"Note"
โ Base Example 2 โ Multilingual Safe Slicing
๐ฅ Input Document
{ "label": "เคจเคฎเคธเฅเคคเฅ" }
๐ Expression
{ "$substrCP": ["$label", 0, 3] }
๐ค Output
"เคจเคฎเคธเฅ"
๐งฑ Ecommerce Example โ Shorten Product Names
๐ Pipeline
[
{ "$unwind": "$items" },
{
"$project": {
"shortName": {
"$substrCP": ["$items.name", 0, 6]
}
}
}
]
๐ฅ Input Document
{
"items": [
{ "name": "Bluetooth Speaker" },
{ "name": "Wireless Keyboard" }
]
}
๐ค Output
[
{ "shortName": "Blueto" },
{ "shortName": "Wirele" }
]
๐ง Common Use Cases
- Multilingual-safe substring extraction
- Character-length-limited output
- Text formatting for display
๐ Related Operators
$substr,$substrBytes,$split,$slice,$strLenCP
๐ง Notes
- Use
$substrCPover$substror$substrBytesfor Unicode strings. - Avoid cutting emojis or multibyte characters with
$substrBytes.