Navigation

MongoDB Service

The MongoDB Stitch requires an MongoDB Atlas-managed cluster. For more information on Atlas, visit MongoDB Atlas, or visit the Atlas documentation.

Integrate an Atlas Cluster with MongoDB Stitch

You can integrate an Atlas cluster with MongoDB Stitch during the creation of a MongoDB Stitch application.

1

Log into Atlas.

To use MongoDB Stitch, you must be logged into MongoDB Atlas. If you do not have an Atlas account, follow the instructions in the Atlas documentation to create an account.

2

Create an Atlas cluster.

If you do not already have an Atlas cluster for use with MongoDB Stitch, create a cluster.

Important

You must deploy the Atlas cluster with MongoDB version 3.4 or greater.

Atlas provides a Free Tier M0 replica set as well as paid M10+ clusters. Free Tier deployments have restrictions as compared to paid M10+ deployments but will work for the purposes of this tutorial. For complete documentation on these restrictions, see Atlas M0 (Free Tier) Limitations.

3

Add a MongoDB Stitch application.

  1. In Atlas, click Stitch Apps in the left-hand navigation.
  2. Click the Create New Application. The name can only contain ASCII letters, numbers, underscores, and hyphens.
  3. Select your Atlas cluster for your MongoDB service.
  4. Click Create.

Upon creation of your app, you will be redirected to the MongoDB Stitch console. Your app is created with a MongoDB service named mongodb-atlas.

4

Add rules for each collection the MongoDB service must access.

You must add rules for each collection the MongoDB service accesses.

  1. Under Atlas Clusters in the sidebar, click on the mongodb-atlas service.

  2. Click the Rules tab.

    You can ignore or delete the default namespace.

  3. Click the New button.

  4. Enter the database name in the Database field.

  5. Enter the collection name in the Collection field.

  6. Click Create.

  7. After you add the namespace, click on the namespace to expand the view.

  8. Configure the rules to control access to fields for read and write operations. MongoDB Stitch creates Default Rules for namespaces.

For complete documentation on the default rules as well as configuring rules, see MongoDB Service Rules.

Pipelines and MongoDB Service

With MongoDB Stitch, you can define a sequence of actions to perform as a pipeline of stages. For the MongoDB service, MongoDB Stitch provides the following actions:

Action Description
aggregate Executes an aggregation pipeline.
delete Deletes documents from a MongoDB collection.
find Queries a collection in MongoDB.
insert Inserts the stage’s input documents into the specified MongoDB collection.
update Modifies one or more documents in a MongoDB collection.

Note

A pipeline can consist of stages that use different MongoDB Stitch services.

Incorporate into a Web Application

To incorporate MongoDB service into a web application:

Note

The following lists the code snippets to incorporate MongoDB service into a web application. The numbered list of items is not meant to be a comprehensive step-by-step procedure. For runnable code examples, see the list of tutorials at the end of the section.

  1. In your HTML file, include the MongoDB Stitch library .

    <script defer type="text/javascript" src="https://s3.amazonaws.com/stitch-sdks/js/library/298a2b586d91d462099e5d9f66fba0a687837abe/stitch.min.js"></script>
    
  2. In your JavaScript file, include the code to instantiate a StitchClient:

    import { StitchClient } from 'stitch';
    
    const stitchClient = new StitchClient('<your-app-id>');
    

    Replace <your-app-id> with your MongoDB Stitch app ID. In the MongoDB Stitch console, you can find your App ID in the Clients view.

  3. StitchClient provides various functions to authenticate. For example, if your MongoDB Stitch application allows Anonymous authentication, include the following code to log in anonymously:

    stitchClient.anonymousAuth();
    
  4. To access MongoDB database and collection using the MongoDB service, use:

    let db = stitchClient.service('mongodb', 'mongodb-atlas').db('<dbname>');
    let myCollection = db.collection('<collection>');
    
    • Replace <dbname> with the database to access.
    • Replace <collection> with the collection in the database to access.

    The Collection provides various CRUD methods for operating on the collection.

    Note

    To perform operations on a collection, you must have defined rules in the MongoDB Stitch console for the collection.

Incorporate into an Android Application

Note

The following lists the code snippets to incorporate MongoDB service into an Android application. The numbered list of items is not meant to be a comprehensive step-by-step procedure. For runnable code examples, see the list of tutorials at the end of the section.

  1. Include the following repositories and depencies in your module-level build.gradle file.

    repositories {
    
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }
    
        // ...
    }
    
    dependencies {
    
       compile 'org.mongodb:stitch:0.1.0-SNAPSHOT'
    
       // ...
    }
    
  2. Instantiate a StitchClient to create a connection to your MongoDB Stitch app. For example:

    final StitchClient _client = new StitchClient(this, "<your-app-id>");;
    

    where "<your-app-id>" is your MongoDB Stitch app ID. In the MongoDB Stitch console, click on Clients in the left-hand navigation pane.

    You can also instantiate a StitchClient object using a properties file stitch.properties which contains the appId set to your MongoDB Stitch app ID. See StitchClient for more information.

  3. Use the logInWithProvider() method to authenticate. For example, if your MongoDB Stitch application allows Anonymous authentication, include the following code to log in anonymously:

    _client.logInWithProvider(new AnonymousAuthProvider())
    
  4. Instantiate a MongoClient to connect to the MongoDB service. For example:

    private MongoClient _mongoClient;
    
    //...
    
    _mongoClient = new MongoClient(_client, "mongodb-atlas");
    
  5. To access a collection:

    Collection coll = _mongoClient.getDatabase("<dbname>").getCollection("<collName>");
    

    For more information, see Database and Collection.

    The Collection class provides various CRUD methods for operating on the collection.

    Note

    To perform operations on a collection, you must have defined rules in the MongoDB Stitch console for the collection.

←   Services MongoDB Rules  →