$indexOfCP - Amazon DocumentDB

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

$indexOfCP

Amazon DocumentDB の $indexOfCP演算子は、文字列式内で指定された部分文字列が最初に出現したときのインデックスをコードポイント (CP) で検索するために使用されます。これは、文字列フィールドからコンテンツを解析および抽出する場合に便利です。

パラメータ

  • string expression: 検索する文字列。

  • substring: 検索する部分文字列。

  • [<start>]: (オプション) 検索を開始する位置 (ゼロベースのインデックス)。デフォルトは 0 です。

例 (MongoDB シェル)

この例では、 $indexOfCP演算子を使用して、各ドキュメントの Desk フィールドでハイフン文字 - の最初の出現のインデックスを見つけます。

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

db.people.insertMany([ { "_id":1, "name":"John Doe", "Manager":"Jane Doe", "Role":"Developer", "Desk": "Düsseldorf-BVV-021"}, { "_id":2, "name":"John Stiles", "Manager":"Jane Doe", "Role":"Manager", "Desk": "Munich-HGG-32a"}, { "_id":3, "name":"Richard Roe", "Manager":"Jorge Souza", "Role":"Product", "Desk": "Cologne-ayu-892.50"}, { "_id":4, "name":"Mary Major", "Manager":"Jane Doe", "Role":"Solution Architect", "Desk": "Dortmund-Hop-78"} ])

クエリの例

db.people.aggregate([ { $project: { stateLocation: { $indexOfCP: [ "$Desk", "-"] } } } ])

出力

{ "_id" : 1, "stateLocation" : 10 } { "_id" : 2, "stateLocation" : 6 } { "_id" : 3, "stateLocation" : 7 } { "_id" : 4, "stateLocation" : 8 }

コードの例

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

Node.js
const { MongoClient } = require('mongodb'); async function main() { 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: { stateLocation: { $indexOfCP: [ "$Desk", "-"] } } } ]).toArray(); console.log(result); await client.close(); } main();
Python
from pymongo import MongoClient 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': { 'stateLocation': { '$indexOfCP': [ '$Desk', '-' ] } } } ])) print(result) client.close()