翻译或纠错本页面

$strLenCP (aggregation)

On this page

Definition

$strLenCP

3.4 新版功能.

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

$strLenCP has the following operator expression syntax:

{ $strLenCP: <string expression> }

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

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

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

Behavior

The $strLenCP operator counts the number of code points in the specified string. This behavior differs from the $strLenBytes operator which counts the number of bytes in the 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:

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

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

db.food.aggregate(
  [
    {
      $project: {
        "name": 1,
        "length": { $strLenCP: "$name" }
      }
    }
  ]
)

The operation returns the following results:

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

参见

$strLenBytes