Docs Menu

$strLenCP (aggregation)

項目一覧

$strLenCP

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

$strLenCPには次の演算子式の構文があります。

{ $strLenCP: <string expression> }

The argument can be any valid that resolves to a string.

引数が null 値に解決されるか、欠落しているフィールドを参照する場合、$strLenCP はエラーを返します。

結果

{ $strLenCP: "abcde" }

5

{ $strLenCP: "Hello World!" }

12

{ $strLenCP: "cafeteria" }

9

{ $strLenCP: "cafétéria" }

9

{ $strLenCP: "" }

0

{ $strLenCP: "$€λA" }

4

{ $strLenCP: "寿司" }

2

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

food コレクションを作成します。

db.food.insertMany( [
{ _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 example uses the $strLenCP operator to calculate the length of each name value:

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

出力例:

[
{ _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 }
]

以下も参照してください。

項目一覧