$regexMatch - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$regexMatch

New from version 5.0. Not supported by Elastic cluster.

The $regexMatch operator in Amazon DocumentDB is used to perform regular expression matching on string fields. It returns a boolean value (true or false) indicating whether the input string matches the specified pattern.

Parameters

  • input: The string to test against the regular expression.

  • regex: The regular expression pattern to match.

  • options: (Optional) Flags to modify the regular expression behavior, such as case-insensitive matching (i) or multiline matching (m).

Example (MongoDB Shell)

The following example demonstrates how to use the $regexMatch operator to check if names start with the letter 'M'. The operator returns true or false for each document.

Create sample documents

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" }, ]);

Query example

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

Output

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

Code examples

To view a code example for using the $regexMatch command, choose the tab for the language that you want to use:

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()