Connecting IBM App Connect to MongoDB

Learn how to connect an integration application in IBM® App Connect to MongoDB.

Note: This tutorial assumes that you have a running App Connect Dashboard instance that was created within an IBM Cloud Pak for Integration environment.

Scenario

You want to connect your integration to MongoDB.

First, find or create everything you need

  1. Download the set of sample files: cp4i-ace-mongodb.zip.

    This ZIP archive contains a BAR file with a basic REST integration and a set of configuration files for setting up the system.

  2. Extract the contents of the ZIP archive to a directory that you can use for working through the tutorial. The ZIP archive contains the following contents:
    • CustomerMongoDBV1.bar: A BAR file that contains a simple REST-based integration that runs CRUD operations on a MongoDB database
    • mongodbloopbackdatasource: A subdirectory that provides the LoopBack connector data source, which contains the following files:
      • datasources.json: A file that contains the connection details to MongoDB
      • mongodbds/customers.json: A file that contains the database model for a MongoDB system
    • mongodbsetdbparms.txt: A file that contains the credentials to use for connecting to MongoDB
    • prepare_and_upload_configurations.sh: A script that shows how to create or update configurations by using the Red Hat® OpenShift® CLI (oc)
  3. Obtain access to a MongoDB system:
    • You will need a MongoDB instance (and cluster) that is accessible from the public internet. This tutorial uses a MongoDB Atlas instance, which is offered as a free tier for trying out MongoDB.
    • Obtain the connection details (Short SRV connection string) for MongoDB. You will need the user, password, and host name from this string for configuring the connection later. The string will be of this form:

      mongodb+srv://USER:PASSWORD@HOSTNAME/test?retryWrites=true

    For further details and other options (such as running MongoDB locally in Docker), see the full tutorial.

Next, create an integration server that is configured to connect to MongoDB

You will now use the App Connect Dashboard to configure an integration server to expose an HTTP endpoint. The example flow is very simple and exposes the following REST API operations:

  • GET on /customers: Gets customer records from a MongoDB customers database.
  • POST on /customers: Creates a customer record in a MongoDB customers database.
  • GET on /customers/{customerId}: Gets a customer record {customerId} from a MongoDB customers database.
  • DELETE on /customers/{customerId}: Deletes a customer record {customerId} from a MongoDB customers database.
  • PUT on /customers/{customerId}: Updates a customer record {customerId} in a MongoDB customers database.

The same configuration could be used for far more complex integrations that include interactions with other external systems.

Before you begin

Update the extracted contents of the cp4i-ace-mongodb.zip file as follows:

  1. From the mongodbloopbackdatasource subdirectory that provides the LoopBack connector data source, edit the datasources.json file, which contains the following content for connecting:
    {
      "mongodbds": {
        "host": "[HOSTNAME]",
        "protocol": "mongodb+srv",
        "port": 27017,
        "database": "CustomerCollection",
        "name": "mongodbds",
        "connector": "mongodb"
      }
    }
    1. Replace [HOSTNAME] with the value found in the Short SRV connection string that you obtained earlier from your MongoDB system.
    2. If the specified standard port of 27017 is different for your MongoDB system, update the port number.
    3. If you encounter the following errors or similar, the SRV records for the database might not be accessible:

      Failed to look up SRV record "_mongodb._tcp.some.domain ": No such file or directory

      To resolve such errors and ensure a successful connection, you might need to set the value of protocol to mongodb instead of mongodb+srv. For more information about the differences between the protocols, see Connection String URI Format in the MongoDB documentation.

  2. Create a ZIP file of the mongodbloopbackdatasource subdirectory (and its updated contents). You will use this ZIP file to create a LoopBack data source configuration later.
  3. Edit the mongodbsetdbparms.txt file, which contains placeholder credentials for connecting to MongoDB:
    loopback::mongodbsi DBUSER DBPASS

    Update the content with your own credentials:

    • Replace DBUSER with the USER value found in the Short SRV connection string that you obtained earlier.
    • Replace DBPASS with the PASSWORD value found in the Short SRV connection string that you obtained earlier.

Procedure

To create the integration server and configure it to connect to MongoDB, complete the following steps:

  1. From your App Connect Dashboard instance, click the Dashboard icon in the navigation pane.
  2. On the Servers page, click Create server.
  3. Choose Quick start integration as the type of integration and click Next.
  4. Provide CustomerMongoDBV1.bar as the BAR file that you want to deploy and click Next.
  5. From the Configuration view, use the Create configuration button to create each of the following configurations in turn:
    1. Select LoopBack data source as the type and upload the ZIP file that you created earlier for the mongodbloopbackdatasource subdirectory. Give the configuration a meaningful name; for example, mongodbloopbackdatasource.
    2. Select setdbparms.txt as the type and upload the mongodbsetdbparms.txt file. Give the configuration a meaningful name; for example, mongodbsetdbparms.
  6. Ensure that all of the new configurations that you created are selected and then click Next.
  7. Define the following details for the integration server:
    1. Give the integration server a meaningful name; for example, mongodbtest.
    2. Ensure that the Designer flows mode is set to disabled, which is required when deploying a Toolkit integration.
    3. Set the application transport for the integration endpoint to http.
    4. Click Create. The integration server should start and expose the HTTP input that drives the integration's REST operations. (You might need to refresh the page to see the Started state on the integration server's tile.)

Finally, test your flow

To test your flow, complete the following steps:

  1. Locate the URLs of the REST API that is exposed by the integration server:
    1. Click the tile for the integration server in the dashboard.
    2. Click the tile for the application within the server.
    3. Click POST /customers and note the displayed curl command example. This operation will be used to add a record to the customers database in MongoDB.
    4. Click GET /customers and note the displayed curl command example. This operation will be used to retrieve all customer records from the customers database in MongoDB.
  2. Run the curl commands to first add records to the database, and then to display the records in the database.

    Also explore the other DELETE, GET, and PUT operations for a record in the database.

Conclusion

This tutorial has shown how to set up an integration server to securely connect to MongoDB.

If preferred, you can use the Red Hat OpenShift CLI to create or update the configurations in the system. The supplied prepare_and_upload_configurations.sh script creates or updates the required configurations by using the oc apply -f command and then causes the integration server to do a rolling restart to pick up the changes. You can modify this script to create any number of different configurations that you require.

To use the script, run oc login to log in to the cluster and project where the Dashboard instance is installed and then execute the script. It will compress the LoopBack connector data source into a ZIP archive and then create or update each configuration object in the cluster.