$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.

CategoryString
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $strLenCP: <string-expression> }

⚙️ Parameters

stringRequired; 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏