

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# \$1RegexMatch
<a name="regexMatch"></a>

Baru dari versi 5.0. Tidak didukung oleh cluster elastis.

`$regexMatch`Operator di Amazon DocumentDB digunakan untuk melakukan pencocokan ekspresi reguler pada bidang string. Ia mengembalikan nilai boolean (`true`atau`false`) menunjukkan apakah string input cocok dengan pola yang ditentukan.

**Parameter**
+ `input`: String untuk menguji terhadap ekspresi reguler.
+ `regex`: Pola ekspresi reguler untuk mencocokkan.
+ `options`: (Opsional) Bendera untuk mengubah perilaku ekspresi reguler, seperti pencocokan case-insensitive (`i`) atau pencocokan multiline (). `m`

## Contoh (MongoDB Shell)
<a name="regexMatch-examples"></a>

Contoh berikut menunjukkan bagaimana menggunakan `$regexMatch` operator untuk memeriksa apakah nama dimulai dengan huruf 'M'. Operator mengembalikan `true` atau `false` untuk setiap dokumen.

**Buat dokumen sampel**

```
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" },

]);
```

**Contoh kueri**

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

**Keluaran**

```
{ _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 }
```

## Contoh kode
<a name="regexMatch-code"></a>

Untuk melihat contoh kode untuk menggunakan `$regexMatch` perintah, pilih tab untuk bahasa yang ingin Anda gunakan:

------
#### [ 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()
```

------