Skip to content

Latest commit

 

History

History
202 lines (159 loc) · 10.1 KB

README.md

File metadata and controls

202 lines (159 loc) · 10.1 KB

MongoDBStitch Examples

This document describes the example applications provided with the MongoDBStitch library. The examples include:

  • DataSender — sending data to MongoDB
  • DataReceiver — receiving data from MongoDB and confirming the receipt

You should run the DataReceiver example after or in parallel with DataSender example. We recommend that you run DataSender on the agent of one imp and run DataReceiver on the agent of a second imp.

Each example is described first; you will find generic and example-specific setup instructions further down the page.

DataSender

This example inserts data into a preconfigured MongoDB collection using a predefined MongoDB Stitch Function:

  • Data is sent every ten seconds.
  • Every data record contains:
    • A "value" attribute. This is an integer value, converted to string, which starts at 1 and increases by 1 with every record sent. It restarts from 1 every time the example is restarted.
    • A "measureTime" attribute. This is an integer value, converted to string, and is the time in seconds since the epoch.

DataSender example

DataReceiver

This example receives new data from a preconfigured MongoDB collection and confirms data receipt using predefined MongoDB Stitch Functions:

  • Data is received every 15 seconds.
  • Every received data record is printed to the log.
  • Data receipt is confirmed by updating the data’s "received" attribute using a predefined MongoDB Stitch Function.

DataReceiver example

Using the Examples

  1. In Electric Imp’s IDE create two new Models, one for imp A (DataSender), the other for imp B (DataReceiver).
  2. Copy the DataSender source code and paste it into the IDE as the agent code of imp A. Before running the code you will need to set the configuration constants as described in the general setup below.
  3. Copy the DataReceiver source code and paste it into the IDE as the agent code of imp B. Before running the code you will need to set the configuration constants as described in the general setup below.
  4. Perform the general setup applicable for the both examples.
  5. Perform additional setup for the DataSender example.
  6. Perform additional setup for the DataReceiver example.
  7. Build and Run DataSender.
  8. Check from the logs in the IDE that data insertions are successful.
  9. Build and Run DataReceiver.
  10. Check from the logs that data is being received successfully.

You may skip the steps related to DataReceiver example if you are only trying the DataSender example.

Setup For All Examples

MongoDB Stitch Account Configuration

In this section the following MongoDB Stitch entities are created and configured:

  • A MongoDB Atlas Cluster.
  • A MongoDB Stitch Application.
  • An API key authentication for the Stitch Application.
  • A MongoDB Collection that will be used to store the Examples data.

Create MongoDB Atlas Cluster

  1. Login to your MongoDB Atlas account in your web browser.
  2. If you have an existing Atlas cluster that you want to work with, skip this step, otherwise:
  3. Click Clusters in the PROJECT menu and click Build a New Cluster: Build a new cluster
  4. In the Build Your New Cluster pop up: 1. Optionally modify Cluster Name. 1. Click Select for M0 Instance Size: Select M0 Cluster 1. Click Confirm & Deploy: Deploy Cluster 1. Wait until your Cluster is deployed. This may take several minutes.

Create MongoDB Stitch Application

  1. Click Stitch Apps in the PROJECT menu and click Create New Application: Create new app
  2. In the Create a new application pop up:
  3. Enter Application Name, eg. testApp.
  4. Choose a Cluster.
  5. Click Create: Create app
  6. Wait until your Application is created. This may take several seconds.
  7. You will be redirected to your Stitch Application page.
  8. Click Clients in the application menu.
  9. Copy (click COPY) the App ID of your Stitch application and paste into a plain text document or equivalent. This will be used as the value of the MONGO_DB_STITCH_APPLICATION_ID constant in the imp agent code: Copy app id

Enable API Key Authentication for the Stitch Application

  1. Click Authentication in the CONTROL menu.
  2. Click Edit for API Keys provider: Edit Api Keys
  3. In the Edit Auth Provider pop up:
  4. Set the API Keys switch to enabled.
  5. Click CREATE API KEY: Create Api Key
  6. Enter a name for the API key into the text input, eg. test.
  7. Click Save: Save Api Key
  8. Copy (click COPY) the ere API key of your Stitch application and paste into a plain text document or equivalent. This will be used as the value of the MONGO_DB_STITCH_API_KEY constant in the imp agent code: Copy Api Key
  9. Close the Edit Auth Provider pop up.

Create a MongoDB Collection

  1. Click mongodb-atlas in the ATLAS CLUSTERS menu and click NEW for MongoDB Collections: New Collection
  2. Enter testdb as the Database name and data as the Collection name.
  3. Click CREATE: Create Collection
  4. Click to your newly created collection: Created Collection
  5. Click the Top-Level Document field: Top-Level Document
  6. Modify the READ permission on top-level document to {}.
  7. Modify the WRITE permission on top-level document to {}.
  8. Click SAVE: Top-Level Document Modification
  9. Click the owner_id field.
  10. Click the delete (X) on the right-hand side: Delete owner_id
  11. In the confirmation popup click Delete Field to confirm the deletion of the owner_id field.
  12. Click SAVE: Collection Rules
  13. Click the Filters tab.
  14. Click DELETE for the existing filter and click SAVE: Collection Filters

Agent Constants Setup

  1. Set the MONGO_DB_STITCH_APPLICATION_ID and MONGO_DB_STITCH_API_KEY constants in the agent example code to the values you copied and saved in the previous steps. Set the same values for both DataSender and DataReceiver: Configuration Constants

Additional Setup for DataSender

This must be performed before you run DataSender.

In this section, the following MongoDB Stitch entities are created and configured:

  • The MongoDB Stitch Function testInsertData that will be used to store DataSender data in the MongoDB Collection configured in the previous steps.

Create MongoDB Stitch Function: testInsertData

  1. In your MongoDB Atlas account select the Stitch Application you created in the previous steps.
  2. Click Functions in the CONTROL menu and click Create New Function: New Function
  3. Enter testInsertData into the Function Name field.
  4. Click the Function Editor tab: testInsertData Function
  5. Enter the following code into the Function Editor text field:
exports = function(data) {
  var collection = context.services.get("mongodb-atlas").db('testdb').collection('data');
  return collection.insertOne({ "data" : data, "received" : false });
};
  1. Click Save: testInsertData code

Additional Setup for DataReceiver

This must be performed before you run DataReceiver.

In this section, the following MongoDB Stitch entities are created and configured:

  • The MongoDB Stitch Function testFindData that will be used to receive data from the MongoDB Collection configured in the previous steps.
  • The MongoDB Stitch Function testConfirmData that will be used to confirm the data has been received.

Create MongoDB Stitch Function: testFindData

  1. In your MongoDB Atlas account select your Stitch Application, created during the previous steps.
  2. Click Functions in the CONTROL menu and click Create New Function: New Function
  3. Enter testFindData into the Function Name field.
  4. Click the Function Editor tab: testFindData Function
  5. Enter the following code into the Function Editor text field:
exports = function() {
  var collection = context.services.get("mongodb-atlas").db('testdb').collection('data');
  return collection.find({ "received" : false }).limit(10).toArray();
};
  1. Click Save: testFindData code

Create MongoDB Stitch Function: testConfirmData

  1. Click Functions in the CONTROL menu and click Create New Function: New Function
  2. Enter testConfirmData into the Function Name field.
  3. Click Function Editor tab: testConfirmData Function
  4. Enter the following code into the Function Editor text field:
exports = function(ids) {
  var collection = context.services.get("mongodb-atlas").db('testdb').collection('data');
  return collection.updateMany({ "_id" : { "$in" : ids } }, { "$set" : { "received" : true } });
};
  1. Click Save: testConfirmData code