$strLenCP (Code-Point Length)
Last updated: 2026-10-09
$strLenCP (Code-Point Length)
$strLenCP returns the number of Unicode code points in a string — essentially the character count people perceive. Chinese, Japanese, and emoji each count as 1, making it right for nickname limits or text summaries. Unlike byte-counting $strLenBytes, multi-byte characters do not inflate the result. Note composite characters (e.g. skin-tone emoji) may span several code points.
| Category | String |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $strLenCP: <string-expression> }
⚙️ Parameters
| string | Required; the string expression to measure. |
|---|
Returns:The code-point length as an integer.
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $strLenCP: validate nickname length
db.users.aggregate([
{ $project: { len: { $strLenCP: '$nickname' } } }
])
Output:
{ "_id": 1, "len": 4 }
{ "_id": 2, "len": 2 }
📌 Notes
- One Chinese character or emoji counts as 1 code point
- Composite characters (e.g. skin-tone emoji) may span multiple code points
- For storage bytes, use $strLenBytes
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhich one for a 20-character nickname limit?
AUse $strLenCP — it counts characters.
QHow far is it from $strLenBytes?
AEach Chinese character differs by 2 bytes.
QHow many is an emoji?
ASimple emoji count as 1; composite ones may count as 2 or more.