1 | initial version |
A variable in regular expressions (regex) for MongoDB's aggregate function can be utilized in several ways:
Example:
const regexPattern = "foo|bar";
db.collection.aggregate([
{
$match: {
field: {
$regex: regexPattern
}
}
}
]);
Example:
const regexPattern = /foo(\d)/;
db.collection.aggregate([
{
$project: {
result: {
$regexFind: {
input: "$field",
regex: regexPattern,
options: "i"
}
}
}
}
]);
Example:
const searchKeyword = "foo";
const regexPattern = new RegExp(".*" + searchKeyword + ".*", "i");
db.collection.aggregate([
{
$match: {
field: {
$regex: regexPattern
}
}
}
]);