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), M2, and M5 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.

Functions and MongoDB Service

With MongoDB Stitch, you can define Javascript functions to perform actions and interact with services. For the MongoDB service, MongoDB Stitch provides the following actions:

Action Description
mongoDBService.db() Retrieve a database.
database.collection() Retrieve a collection.
collection.find() Queries a collection in MongoDB.
collection.findOne() Queries a collection in MongoDB and returns at most one result.
collection.insertOne() Inserts a new document in a collection.
collection.insertMany() Inserts multiple new documents in a collection.
collection.updateOne() Modifies a single document in a collection.
collection.updateMany() Modifies multiple documents in a collection.
collection.upsert() Updates an existing document or inserts a new document in a collection.
collection.deleteOne() Deletes a single document in a collection.
collection.deleteMany() Deletes multiple documents in a collection.
collection.aggregate() Provides access to the aggregation pipeline.

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 src="https://s3.amazonaws.com/stitch-sdks/js/library/stable/stitch.min.js"></script>
    
  2. In your JavaScript file, include the code to instantiate a StitchClient:

    const stitchClient = new stitch.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.login();
    
  4. To access MongoDB database and collection using the MongoDB service, use:

    const db = stitchClient.service('mongodb', 'mongodb-atlas').db('<dbname>');
    const 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 dependencies 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.