$strLenCP - Amazon DocumentDB

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

$strLenCP

Amazon DocumentDB の $strLenCP演算子は、コードポイントの文字列式の長さ (Unicode 文字) を決定するために使用されます。これは、バイト数ではなく文字列内の文字数を知る必要がある場合に便利です。

パラメータ

  • expression: コードポイントで長さを返す文字列式。

例 (MongoDB シェル)

次の例は、 $strLenCP演算子を使用して Unicode 文字を含む文字列の長さを決定する方法を示しています。

サンプルドキュメントを作成する

db.people.insertMany([ { "_id": 1, "Desk": "Düsseldorf-BVV-021" }, { "_id": 2, "Desk": "Munich-HGG-32a" }, { "_id": 3, "Desk": "Cologne-ayu-892.50" }, { "_id": 4, "Desk": "Dortmund-Hop-78" } ])

クエリの例

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

出力

{ "_id" : 1, "Desk" : "Düsseldorf-BVV-021", "length" : 18 } { "_id" : 2, "Desk" : "Munich-HGG-32a", "length" : 14 } { "_id" : 3, "Desk" : "Cologne-ayu-892.50", "length" : 18 } { "_id" : 4, "Desk" : "Dortmund-Hop-78", "length" : 15 }

Unicode 文字 (Ü) を含む「Düsseldorf-BVV-021」文字列の長さ測定の違いに注目してください。$strLenCP 演算子は Unicode 文字数を正しくカウントし、$strLenBytes演算子はバイト数をカウントします。

コードの例

$strLenCP コマンドを使用するコード例を表示するには、使用する言語のタブを選択します。

Node.js

以下は、MongoDB ドライバーで Node.js アプリケーションで $strLenCP演算子を使用する例です。

const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const collection = db.collection('people'); const result = await collection.aggregate([ { $project: { "Desk": 1, "length": { $strLenCP: "$Desk" } } } ]).toArray(); console.log(result); await client.close(); } example();
Python

PyMongo ドライバーで Python アプリケーションで $strLenCP演算子を使用する例を次に示します。

from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client.test collection = db.people result = list(collection.aggregate([ { '$project': { "Desk": 1, "length": { "$strLenCP": "$Desk" } } } ])) print(result) client.close() example()