Navigation

Build a Local-Only Mobile App

Deprecation

MongoDB has deprecated MongoDB Mobile. We are focusing our efforts on Realm, the most popular database for mobile devices.

Stay up to date and see what we have planned.

Overview

After setting up your Android or iOS project to use the MongoDB Stitch SDKs, you are ready to initialize MongoDB Stitch and connect to the database. The following procedure shows you how to build an app that only uses MongoDB Mobile for data storage. For an example that uses both Atlas and MongoDB Mobile in an app, see Build a Mobile App with Sync.

Prerequisites

If you have not done so yet, refer to the steps in Set up a MongoDB Mobile Project to set up your Android or iOS project to use the MongoDB Stitch SDKs.

Initializing MongoDB Mobile

The following code demonstrates initializing and accessing the local MongoDB Mobile database.

  1. Include the necessary packages:

    // Base Stitch Packages
    import com.mongodb.stitch.android.core.Stitch;
    import com.mongodb.stitch.android.core.StitchAppClient;
    
    // Packages needed to interact with MongoDB and Stitch
    import com.mongodb.client.MongoClient;
    import com.mongodb.client.MongoCollection;
    
    // Necessary component for working with MongoDB Mobile
    import com.mongodb.stitch.android.services.mongodb.local.LocalMongoDbService;
    
  2. Initialize the MongoDB Mobile database, and then create a MongoDB Mobile client in the application:

    // Create the default Stitch Client
    final StitchAppClient client =
       Stitch.initializeDefaultAppClient("<APP ID>");
    
    // Create a Client for MongoDB Mobile (initializing MongoDB Mobile)
    final MongoClient mobileClient =
       client.getServiceClient(LocalMongoDbService.clientFactory);
    
  3. You can now access MongoDB Mobile directly. For example, to point to a collection and insert a document, use the following code:

    // Point to the target collection and insert a document
    MongoCollection<Document> localCollection =
       mobileClient.getDatabase("my_db").getCollection("my_collection");
    
    localCollection.insertOne(document);
    
    // Find the first document
    Document doc = localCollection.find().first();
    
    //Find all documents that match the find criteria
    Document query = new Document();
    query.put("name", new BsonString("veirs"));
    
    FindIterable<Document> cursor = localCollection.find(query);
    ArrayList<Document> results =
       (ArrayList<Document>) cursor.into(new ArrayList<Document>());
    

In your project, add the following code to initialize the MongoDB Stitch client and MongoDB Mobile connection:

// import the necessary modules to work with Stitch and MongoDB Mobile
import StitchCore
import StitchLocalMongoDBService

// Create the default Stitch Client
let client = try Stitch.initializeDefaultAppClient(
   withClientAppID: "<APP ID>"
)

// Create a Client for MongoDB Mobile (initializing MongoDB Mobile)
let localMongoClient =
   try client.serviceClient(fromFactory: mongoClientFactory)

Your code can now access MongoDB Mobile directly. For example, to point to a collection and insert a document, use the following code:

let doc: Document = [
    "me": "cool",
    "numbers": 42
]

// Point to the target collection and insert a document
let myLocalCollection = try localMongoClient.db("my_db")
   .collection("my_collection")
let result = try myLocalCollection.insertOne(doc)