Navigation

MongoDB Service Filters

For a MongoDB Service, you can specify filters that are applied to documents when a specified condition is met. These filters are in addition to the query predicates for read and write operations.

Default Filter

By default, the collection has the following filter rule:

When Match Expression
{ "%%true": true } { "owner_id": "%%user.id" }

This filter indicates that when %%true equals true (i.e. always), apply the following filter { "owner_id": "%%user.id" } to the read and write operations. This filter is in addition to the query predicates for read and write operations.

Filter Syntax

For a filter rule, you need to specify both:

  • A When condition that indicates when to apply the filter, and
  • A Match Expression filter to apply to read and write operations.

When Expression Syntax

The syntax for the When condition is a JSON document. MongoDB expression operators (with the exception of the %text and the geospatial operators) are available.

Important

Because the filter is applied before any documents are read, you can only specify MongoDB Stitch values, named pipelines, or generally available expansions (e.g. %%true, %%user, etc) and literals. You cannot refer to a document field in the when expression.

{
  <value|namedpipeline|general expansion>: <literal1|expression1|value|namedpipeline|general expansion>,
  ...
}

Match Expression Syntax

The syntax for the Match Expression filter is similar to the MongoDB query predicate. MongoDB expression operators (with the exception of the %text and the geospatial operators) are available.

Important

You cannot prefix the field name with an expansion.

{
  <field1>: <literal1|expression1|value|namedpipeline|general expansion>,
  <field2>: <literal2|expression2|value|namedpipeline|general expansion>,
  ...
}

or

{
  "%or": [
     { "<field1>": <literal1|exp1|value|namedpipeline|general expansion> },
     { "<field2>": <literal2|exp2|value|namedpipeline|general expansion> },
     ...
  ]
}

Filter Behavior

When you specify multiple filter conditions, for each when expression that evaluates to true, the corresponding Match Expression filter is applied.

Filter Example

A collection reports contains the following documents:

{
   "_id" : ObjectID("58c96c02fc2c7dea5720e1e0"),
   "title" : "Pies",
   "about" : { "subject" : "pies", "counts" : { "pages" : 5, "words" : 100 } },
   "classification" : "Public",
   "views" : 101
}
{
   "_id" : ObjectID("58c96c0bfc2c7dea5720e24b"),
   "title" : "Pastries Part 1",
   "about" : { "subject" : "puff pastries", "counts" : { "pages" : 50, "words" : 5000 } },
   "classification" : "Public",
   "views" : 21
}
{
  "_id" : ObjectID("58c96c13fc2c7dea5720e272"),
  "title" : "Cakes",
  "about" : { "subject" : "cupcakes", "counts" : { "pages" : 1, "words" : 200 } },
  "classification" : "Internal",
  "views" : 50
 }

Specify the following two filter rules:

When Match Expression Description
{ }
{ "views": { "$gte": 50 } }
MongoDB Stitch always applies an additional query predicate of views greater than or equal to 50 for all read and write operations.
{ "%%user.idHex": "589a86bcfc57bd5c25c51de8" }
{ "views": { "$gte": 20 } }
If the user logged into MongoDB Stitch has the id value of "589a86bcfc57bd5c25c51de8" , MongoDB Stitch applies an additional query predicate of views greater than 20 for all read and write operations.

For all read and write operations, MongoDB Stitch applies the views: { $gte: 50 } filter. In addition, if the user logged into MongoDB Stitch has the specified id value, MongoDB Stitch also applies the views: { $gte: 20 } filter. That is, for the user that meets both When conditions, the filter applied is:

{ "$and": [ { "views": { "$gte": 50 } }, { "views": { "$gte": 20 } } ] }