$regexFindAll - Amazon DocumentDB

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

$regexFindAll

5.0 で導入

Amazon DocumentDB の $regexFindAll演算子は、ドキュメント内の文字列フィールドで正規表現マッチングを実行するために使用されます。これにより、特定の正規表現パターンに一致する特定の部分文字列を検索して抽出し、正規表現のすべての一致を返すことができます。

パラメータ

  • input: 検索する文字列フィールドまたは式。

  • regex: 一致する正規表現パターン。

  • options: (オプション) 大文字と小文字の区別や複数行の一致など、正規表現のオプションパラメータを指定するオブジェクト。サポートされているオプションは、 i (大文字と小文字を区別しない) と m (複数行) です。

例 (MongoDB シェル)

次の例は、 $regexFindAll演算子を使用して emailフィールドからすべての文字シーケンスを抽出する方法を示しています。

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

db.users.insertMany([ { _id: 1, name: "John Doe", email: "john@example.com", phone: "555-1234" }, { _id: 2, name: "Jane Roe", email: "jane@example.com", phone: "555-5678" }, { _id: 3, name: "Carlos Salazar", email: "carlos@example.com", phone: "555-3456" }, { _id: 4, name: "Saanvi Sarkar", email: "saanvi@example.com", phone: "555-7890" } ]);

クエリの例

db.users.aggregate([ { $project: { name: 1, emailMatches: { $regexFindAll: { input: '$email', regex: '[a-z]+', options: 'i' } } } } ])

出力

[ { _id: 1, name: 'John Doe', emailMatches: [ { match: 'john', idx: 0, captures: [] }, { match: 'example', idx: 5, captures: [] }, { match: 'com', idx: 13, captures: [] } ] }, { _id: 2, name: 'Jane Roe', emailMatches: [ { match: 'jane', idx: 0, captures: [] }, { match: 'example', idx: 5, captures: [] }, { match: 'com', idx: 13, captures: [] } ] }, { _id: 3, name: 'Carlos Salazar', emailMatches: [ { match: 'carlos', idx: 0, captures: [] }, { match: 'example', idx: 7, captures: [] }, { match: 'com', idx: 15, captures: [] } ] }, { _id: 4, name: 'Saanvi Sarkar', emailMatches: [ { match: 'saanvi', idx: 0, captures: [] }, { match: 'example', idx: 7, captures: [] }, { match: 'com', idx: 15, captures: [] } ] } ]

注: クエリで Amazon DocumentDB プランナーバージョン 1 を使用している場合は、ヒントを使用してインデックスを使用する必要があります。ヒントがない場合、クエリはコレクションスキャンを実行する場合があります。プランナーのバージョンを確認し、ヒントの使用の詳細については、「[Amazon DocumentDB クエリプランナードキュメント](https://docs.aws.amazon.com/documentdb/latest/developerguide/query-planner.html)」を参照してください。

コードの例

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

Node.js

Node.js アプリケーションで $regexFind演算子を使用する例を次に示します。

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 users = db.collection('users'); const results = await users.aggregate([ { $project: { name: 1, emailMatches: { $regexFindAll: { input: "$email", regex: "[a-z]+", options: "i" } } } } ]).toArray(); console.log(JSON.stringify(results, null, 2)); await client.close(); } main();
Python

Python アプリケーションで $regexFind演算子を使用する例を次に示します。

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'] users = db['users'] results = list(users.aggregate([ { "$project": { "name": 1, "emailMatches": { "$regexFindAll": { "input": "$email", "regex": "[a-z]+", "options": "i" } } } } ])) print(results) client.close()