$substrBytes - Amazon DocumentDB

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

$substrBytes

Amazon DocumentDB の $substrBytes演算子は、指定されたバイト範囲に基づいて文字列から部分文字列を抽出するために使用されます。この演算子は、文字列から部分文字列を抽出する必要があり、文字列内の各文字を表すために必要なバイト数が重要である場合に便利です。

Unicode コードポイントの数で動作する とは異なり$substrCP、 は文字列内の文字を表すために必要なバイト数で$substrBytes動作します。これは、ASCII 以外の文字を含む文字列を使用する場合に特に便利です。これらの文字では、表すために複数のバイトが必要になる可能性があるためです。

*注:* $substrはバージョン 3.4 以降廃止されました。 $substrは のエイリアスになりました$substrBytes

パラメータ

  • string: 部分文字列が抽出される入力文字列。

  • startByte: 抽出する部分文字列のゼロベースの開始バイト位置。負の値を使用して、文字列の末尾から位置を指定できます。

  • length: 抽出する部分文字列のバイト数。

例 (MongoDB シェル)

この例では、 $substrBytes を使用して、ASCII 以外の文字を含む文字列から部分文字列を抽出します。

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

db.people.insertMany([ { "_id": 1, "Desk": "Düsseldorf-NRW-021" }, { "_id": 2, "Desk": "Bremerhaven-HBB-32a" }, { "_id": 3, "Desk": "Norderstedt-SHH-892.50" }, { "_id": 4, "Desk": "Brandenburg-BBB-78" } ]);

クエリの例

db.people.aggregate([ { $project: { "state": { $substrBytes: [ "$Desk", 12, 3] } } } ])

出力

{ "_id": 1, "state": "NRW" }, { "_id": 2, "state": "HBB" }, { "_id": 3, "state": "SHH" }, { "_id": 4, "state": "BBB" }

この例では、 $substrBytesを使用して、 Deskフィールドの 12 バイト目から 3 バイトの部分文字列を抽出します。これにより、文字列に ASCII 以外の文字が含まれている場合でも、2 文字の状態の略語を抽出できます。

コードの例

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

Node.js
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 people = db.collection('people'); const result = await people.aggregate([ { $project: { "state": { $substrBytes: ["$Desk", 12, 3] } } } ]).toArray(); console.log(result); client.close(); } example();
Python
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 people = db.people result = list(people.aggregate([ { '$project': { "state": { '$substrBytes': ["$Desk", 12, 3] } } } ])) print(result) client.close() example()