Navigation

Add MongoDB to Your Stitch App

Overview

The following steps guide you through integrating a MongoDB instance to your MongoDB Stitch application.

Prerequisites

Before you begin, you will need an existing MongoDB Stitch application. If you have not set one up yet, see Creating a Stitch Application.

Procedure

A. Add a MongoDB Instance

When connecting your MongoDB Stitch application to a MongoDB instance, you specify a namespace for each collection the Stitch service accesses. A namespace is the combination of the names of a database and collection. In Stitch, each namespace contains rules and filters that control access to the documents during read and write operations. You define these rules in the Stitch UI when you add a new MongoDB instance.

To add a MongoDB instance, follow these steps:

  1. In the left navigation pane, under the ATLAS CLUSTERS heading, click on the cluster you want to use.
  2. Click the Add Collection button to open the Add New Collection window.
  3. Enter a Database name and Collection name. If you do not want to define any rules for this collection yet, select No template, and then click Add Collection.

Your new collection displays in the list of collections. For example, if you create a new collection for the Artists collection on the MyMusic database, the result might look like this:

Screenshot of setting up a new collection.

You can also create a new collection from the Getting Started screen in the second box; however, you can not select a template to use with the collection.

B. Create a Collection’s Rules

A collection’s rules constrain what data users can read and/or write. A rule consists of a role (a user group) and permissions assigned to that role. The rule may also include schema validation and filters, which limit the amount of data returned from MongoDB.

To learn more about rules, see MongoDB Rules.

To create a new rule, follow these steps:

  1. Follow the steps above to create a new collection.
  2. Select a template from the list. You can also select No template, which prevents both Read and Write access on the collection.
  3. Provide a name for the Role you are creating, or accept the default value.
  4. If the template you selected uses a user ID to restrict access, you will be prompted to identify the field in the collection that contains the user id. If the template also includes a Share List, specify the field that contains the IDs of users who can read the document.
  5. Click Add Collection.

In the following example, we are creating a new collection called MyMusic.Artists. This collection will have a rule that allows users to read any data in the collection, but only write their own data. The Rule will map permissions to the createdBy field in the MyMusic.Artists collection.

Screenshot of setting up a new collection.

While a rule created with a template will likely meet your needs, you may need to customize it. To learn more about managing rules, see Managing MongoDB Rules.