MongoDB Flows

Estimated reading time: 6 minutes

Flows supports connectivity to MongoDB. MongoDB nodes allow all major operations on the MongoDB database.

Prerequisites:

Install MongoDB through OT Link Platform Marketplace

To install MongoDB on the OT Link Platform device:

  1. Go to Applications > Marketplace in the navigation panel.
  2. Search for MongoDB. Click the card to begin installing the application.

  3. Enter a name for the application. Leave the other fields as their default values. Optionally, enter a description.

  4. Click Launch. Go to Applications > Overview to ensure that the mongoDB app is running without any errors.

TroubleShooting:

  • Check System > Info to make sure that you have enough space on your OT Link Platform device.
  • Make sure that you do not have any network connectivity issues.
  • Click the MongoDB card, then scroll down to view the logs to view the specific error

Create the Flows

The following sections will show how to insert values into a MongoDB database, read the inserted values, update the values, and remove the values from the database.

There are four different types of MongoDB nodes:

  • Choose Mongodb in for simple queries, such as find and count
  • Choose Mongodb out for simple operations, such as insert, update, _and _remove.
  • Choose Mongodb2 and Mongodb3 for more complex operations, like mapreduce.

Configure the MongoDB Node

Before creating MongoDB flows, you must configure a database connection. You will only need to configure this once to use the same configuration in all flows.

To configure MongoDB nodes:

  1. Drag any Mongodb node to the canvas.
  2. Double-click the node. Enter any name for the collection, MongoDB will create it if it does not already exist. 
  3. Click  to edit the configuration. Enter the IP address for your MongoDB server. It will be 127.0.0.1 if your MongoDB instance is running on the same device as OT Link Platform.
  4. Enter 27017 for the port, unless you changed it to a different value from the default one.
  5. Enter any name for the database. MongoDB will create it if it does not already exist. MongoDB does not require a username or password by default, so you can leave this blank unless you set them previously.

  6. Click Update, then click Done to save the configuration.

Insert Values

To insert new records into a MongoDB database:

  1. Connect an Inject node, a Function node, and a Mongodb out node.

  2. Double-click the Function node and enter the following lines:

    msg.payload = {
      "column1": "value1",
      "column2": 15
    }; // Change to be the keys and values appropriate for your database.
    return msg;
    
  3. Click Done. Double-click the Mongodb out node. Select insert as the Operation. Make sure to check Only store msg.payload object.

  4. Click Done. Click Save.
  5. Click the Inject node button to insert records into the database. Each click will insert a new record with a new _id.

Read all Records

To retrieve all records from a MongoDB collection:

  1. Connect an Inject node, a Mongodb in node, and a debug node.

  2. Double-click the Mongodb in node. Enter the name of your collection. Select find as the Operation.

  3. Click Done. Click Save. Click the Inject node button to read all records in the database collection.
  4. View the output in the Debug tab.

Read a Single Record

To retrieve single record from a MongoDB collection:

  1. Connect an Inject node, a Function node, a Mongodb2 node, and a Debug node.

  2. Double-click the Mongodb2 node. Select External Service as the Service. Enter the database server information like: mongodb://<ipaddress>:<port>/<databasename>

  3. Select findOne as the Operation. Click Done.
  4. Double-click the Function node. Enter the following lines:

    msg.payload = {
      "column2": 16
    } // This line will read all records where column2 is equal to 16.
    return msg;
    
  5. Click Done. Click Save.
  6. Click the Inject node button to read the record that matches the specified key-value pair. View the record in the Debug tab.

Update Values

To update records in a MongoDB database:

  1. Connect an Inject node, a Function node, and a Mongodb out node.

  2. Double-click the Mongodb out node. Type your Collection. Select update as the Operation. Make sure to check Update all matching documents to update multiple records.

  3. Double-click the Function node. Enter the following lines:

    msg.query = {
      "column2": 15
    }; // This line makes the query only update records where column2 is equal to 15.
    msg.payload = {
      "$set": {
        "column2": 20
      }
    }; // This line uses the set operator to ensure that the update will set column2 to 20 for all matching records.
    return msg;
    
  4. Click Done. Click Save. Click the Inject node button to perform the update operation.
  5. (Optional) Try a find operation to confirm the result of the update.

Delete Values

To delete records from a MongoDB database:

  1. Connect an Inject node, a Function node, and a Mongodb out node.

  2. Double-click the Mongodb out node. Type in your Collection. Select remove as the Operation. Click Done.

  3. Double-click the Function node. Enter the following lines:

    msg.payload = {
      "column2": 20
    }; // Makes the remove operation delete all records where column2 equals 20.
    return msg;
    
  4. Click Done. Click Save. Click the Inject node button to perform the remove operation.
  5. (Optional) Try a find operation to confirm the result of the delete.