$strLenCP (aggregation)

Definition

  • $strLenCP

New in version 3.4.

Returns the number of UTF-8 code points in the specified string.

$strLenCP has the following operator expressionsyntax:

  1. { $strLenCP: <string expression> }

The argument can be any valid expression as long as it resolves to an string. Formore information on expressions, see Expressions.

If the argument resolves to a value of null or refers to amissing field, $strLenCP returns an error.

ExampleResults{ $strLenCP: "abcde" }5{ $strLenCP: "Hello World!" }12{ $strLenCP: "cafeteria" }9{ $strLenCP: "cafétéria" }9{ $strLenCP: "" }0{ $strLenCP: "$€λA" }4{ $strLenCP: "寿司" }2

Behavior

The $strLenCP operator counts the number of code pointsin the specified string. This behavior differs from the$strLenBytes operator which counts the number of bytes inthe string, where each character uses between one and four bytes.

Example

Single-Byte and Multibyte Character Set

A collection named food contains the following documents:

  1. { "_id" : 1, "name" : "apple" }
  2. { "_id" : 2, "name" : "banana" }
  3. { "_id" : 3, "name" : "éclair" }
  4. { "_id" : 4, "name" : "hamburger" }
  5. { "_id" : 5, "name" : "jalapeño" }
  6. { "_id" : 6, "name" : "pizza" }
  7. { "_id" : 7, "name" : "tacos" }
  8. { "_id" : 8, "name" : "寿司" }

The following operation uses the $strLenCP operator to calculatethe length of each name value:

  1. db.food.aggregate(
  2. [
  3. {
  4. $project: {
  5. "name": 1,
  6. "length": { $strLenCP: "$name" }
  7. }
  8. }
  9. ]
  10. )

The operation returns the following results:

  1. { "_id" : 1, "name" : "apple", "length" : 5 }
  2. { "_id" : 2, "name" : "banana", "length" : 6 }
  3. { "_id" : 3, "name" : "éclair", "length" : 6 }
  4. { "_id" : 4, "name" : "hamburger", "length" : 9 }
  5. { "_id" : 5, "name" : "jalapeño", "length" : 8 }
  6. { "_id" : 6, "name" : "pizza", "length" : 5 }
  7. { "_id" : 7, "name" : "tacos", "length" : 5 }
  8. { "_id" : 8, "name" : "寿司", "length" : 2 }

See also

$strLenBytes