Regular Expressions in Filters
On this page
New in version 1.6.
Starting in 1.6, the start
API now supports the use of
Regular Expressions to configure filters for the includeNamespaces
and excludeNamespaces
parameters used in Filtered Sync.
Syntax
To match databases and collections for mongosync
to use Filtered Sync,
you can use regular expressions:
{ "databaseRegex": { "pattern": "<string>", "options": "<string>" }, "collectionsRegex": { "pattern": "<string>", "options": "<string>" } }
The regular expression pattern you pass into a filter must follow the regex
syntax supported by the MongoDB server.
Regular expressions in filter documents use options
listed in the regex
guide. options
is a string of concatenated options. For example, to specify
the i
and s
options, pass in "si"
to options
. The order of concatenated
options does not matter.
Regular expressions in filter documents use the following fields:
Option | Type | Description |
---|---|---|
| document | Specifies which collections you want the filter to match. |
| string | Regular expression options to use in the match. |
| string | Regular expression pattern to match. |
| document | Specifies which databases you want the filter to match. |
| string | Regular expression options to use in the match. |
| string | Regular expression pattern to match. |
These options are available to use with both the includeNamespaces
and excludeNamespaces
parameters.
Use Cases
Regular expressions allow you match multiple databases or collections with a single pattern. If you want to match multiple similarly named databases or collections, a regular expression may be easier to match than creating a series of filters for individual databases or groups of collections.
Details
Regular Expression Options Example
databaseRegex
and collectionsRegex
each supports an options
field,
which you can use to configure regular expression options.
Internally, mongosync
passes the filter and options to the
$regex
operator. Options available to that operator can be used
with Filtred Sync.
For example, this filter would match collections in the sales
database
that begin with the accounts_
string. The filter also specifies the option m
to
match characters at the beginning or end of each line for strings with multiline values, and the
option s
to allow the dot character to match all characters including newline
characters.
"includeNamespaces": [ { "database": "sales", "collectionsRegex": { "pattern": "^accounts_.+?$", "options": "ms" } } ]