$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: