length

Returns the number of code points in the specified string.

If the argument resolves to null, this function returns null.

Counting characters

This function uses MongoDB's $strLenCP operator, which counts characters using Unicode code points. This differs from Kotlin's String.length, which uses UTF-16 code units. For strings containing characters outside the Basic Multilingual Plane (like emoji or certain mathematical symbols), the counting behavior will differ.

For example, the emoji "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ" (family) is a single Unicode grapheme cluster but consists of multiple code points. According to this operator, it has a length of 7. However, according to Kotlin's String.length, it has a length of 11.

Example

class Document(
val text: String,
val length: Int,
)

collection.aggregate()
.set {
Document::length set of(Document::text).length
}.toList()

External resources

See also