このバージョンのドキュメントはアーカイブされており、サポートされなくなりました。5.0 配置をアップグレードするには、 MongoDB 6.0 のアップグレード手順 を参照してください。
定義
$strLenCPバージョン 3.4 で追加。
指定された string 内の UTF-8 コード ポイント の数を返します。
{ $strLenCP: <string expression> } 引数は、文字列に変換されるなら、どのような有効な式でも使用できます。
引数が
null値に解決されるか、欠落しているフィールドを参照する場合、$strLenCPはエラーを返します。例結果{ $strLenCP: "abcde" }5{ $strLenCP: "Hello World!" }12{ $strLenCP: "cafeteria" }9{ $strLenCP: "cafétéria" }9{ $strLenCP: "" }0{ $strLenCP: "$€λA" }4{ $strLenCP: "寿司" }2
動作
$strLenCP演算子は、指定された string 内のコード ポイントの数をカウントします。 この動作は、string 内のバイト数をカウントする$strLenBytes演算子とは異なります。ここでは、各文字は 1 から 4 バイトの間を使用します。
例
シングルバイトおよびマルチバイト文字セット
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: "寿司" } ] )
次の例では、$strLenCP 演算子を使用して、各 name 値の length を計算します。
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 } ]