Navigation

MongoDB Filters

In the MongoDB service in Stitch, filters are applied to queries to reduce the number of records that are returned from a query, or to redact specific fields from those documents. These filters are in addition to the query predicates for read and write operations. There are three primary reasons why you should use filters:

  1. Filters make CRUD operations more efficient by only querying for the specific documents the user has permission to access.
  2. Filters can take advantage of indexes.
  3. Filters can be used to redact specific fields.

Filter Syntax

For a filter, you need to specify both:

  • An apply_when condition that indicates when to apply the filter, and
  • A query filter to apply to read and write operations.

apply_when Expression Syntax

The syntax for the apply_when condition is a JSON document. All MongoDB expression operators except the $text and geospatial operators are available in Stitch.

Important

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

query Expression Syntax

The syntax for the query filter is similar to the MongoDB query predicate. All MongoDB expression operators except the $text and geospatial operators are available.

Important

You cannot prefix the field name with an expansion.

Filter Behavior

When you specify multiple filter conditions, for each apply_when expression that evaluates to true, the corresponding query 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
 }

Let’s suppose we want to apply two filters when querying this collection:

  1. Only show documents that have been viewed more than 50 times (the value of the “views” field is greater than 50).
  2. If a specific user is making the request, show documents that have been viewed more than 20 times.

To accomplish this, you can 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 } } ] }