$regexMatch - Amazon DocumentDB

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

$regexMatch

バージョン 5.0 の新機能。Elastic クラスターではサポートされていません。

Amazon DocumentDB の $regexMatch演算子は、文字列フィールドで正規表現マッチングを実行するために使用されます。入力文字列が指定されたパターンと一致するかどうかを示すブール値 (true または false) を返します。

パラメータ

  • input: 正規表現に対してテストする文字列。

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

  • options: (オプション) 大文字と小文字を区別しないマッチング () や複数行マッチング (i) など、正規表現の動作を変更するフラグm

例 (MongoDB シェル)

次の例は、 $regexMatch演算子を使用して、名前が文字「M」で始まるかどうかを確認する方法を示しています。演算子は、ドキュメントfalseごとに trueまたは を返します。

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

db.users.insertMany([ { "_id":1, name: "María García", email: "maría@example.com" }, { "_id":2, name: "Arnav Desai", email: "arnav@example.com" }, { "_id":3, name: "Martha Rivera", email: "martha@example.com" }, { "_id":4, name: "Richard Roe", email: "richard@example.com" }, ]);

クエリの例

db.users.aggregate([ { $project: { name: 1, startsWithM: { $regexMatch: { input: "$name", regex: "^M", options: "i" } } } } ]);

出力

{ _id: 1, name: 'María García', startsWithM: true }, { _id: 2, name: 'Arnav Desai', startsWithM: false }, { _id: 3, name: 'Martha Rivera', startsWithM: true }, { _id: 4, name: 'Richard Roe', startsWithM: false }

コードの例

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

Node.js
const { MongoClient } = require('mongodb'); async function checkNamePattern() { 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('users'); const result = await collection.aggregate([ { $project: { name: 1, startsWithM: { $regexMatch: { input: "$name", regex: "^M", options: "i" } } } } ]).toArray(); console.log(result); await client.close(); } checkNamePattern();
Python
from pymongo import MongoClient def check_name_pattern(): 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.users result = list(collection.aggregate([ { '$project': { 'name': 1, 'startsWithM': { '$regexMatch': { 'input': '$name', 'regex': '^M', 'options': 'i' } } } } ])) print(result) client.close() check_name_pattern()