

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

# \$1regexFind
<a name="regexFind"></a>

バージョン 5.0 の新機能。

Elastic クラスターではサポートされていません。

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

**パラメータ**
+ `input`: 検索する文字列フィールドまたは式。
+ `regex`: 一致する正規表現パターン。
+ `options`: (オプション) 大文字と小文字の区別や複数行の一致など、正規表現のオプションパラメータを指定するオブジェクト。サポートされているオプションは、 `i` (大文字と小文字を区別しない) と `m` (複数行) です。

## 例 (MongoDB シェル)
<a name="regexFind-examples"></a>

次の例は、 `$regexFind`演算子を使用して、 `name`フィールドが特定の正規表現パターンに一致するドキュメントを検索する方法を示しています。

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

```
db.users.insertMany([
  { "_id": 1, name: "John Doe", email: "john@example.com" },
  { "_id": 2, name: "Diego Ramirez", email: "diego@example.com" },
  { "_id": 3, name: "Alejandro Rosalez", email: "alejandro@example.com" },
  { "_id": 4, name: "Shirley Rodriguez", email: "shirley@example.com" }
]);
```

**クエリの例**

```
db.users.aggregate([
  {
    $project: {
      names: {
        $regexFind: { input: '$name', regex: 'j', options: 'i' }
      }
    }
  },
  { $match: {names: {$ne: null}}}
])
```

このクエリは、 `name`フィールドに「j」という文字が含まれているすべてのドキュメントを返します (大文字と小文字は区別されません）。

**出力**

```
[
  { _id: 1, names: { match: 'J', idx: 0, captures: [] } }
]
```

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

## コードの例
<a name="regexFind-code"></a>

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

------
#### [ 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 users = db.collection('users');

  const results = await users.aggregate([
    { $project: { names: { $regexFind: { input: "$name", regex: "john", options: "i" }}}},
    { $match: {names: {$ne: null}}}
  ]).toArray();
  

  console.log(results);

  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']
users = db['users']

results = list(users.aggregate([
    { "$project": { "names": { "$regexFind": { "input": "$name", "regex": "john", "options": "i" }}}},
    { "$match": {"names": {"$ne": None}}}
]))

print(results)

client.close()
```

------