Connecting IBM App Connect to MongoDB
Learn how to connect an integration application in IBM® App Connect to MongoDB.
Scenario
You want to connect your integration to MongoDB.
First, find or create everything you need
- 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.
- 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)
- 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 MongoDBcustomers
database.POST
on /customers: Creates a customer record in a MongoDBcustomers
database.GET
on /customers/{customerId}: Gets a customer record {customerId} from a MongoDBcustomers
database.DELETE
on /customers/{customerId}: Deletes a customer record {customerId} from a MongoDBcustomers
database.PUT
on /customers/{customerId}: Updates a customer record {customerId} in a MongoDBcustomers
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:
- 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" } }
- Replace [HOSTNAME] with the value found in the
Short SRV connection string
that you obtained earlier from your MongoDB system. - If the specified standard port of 27017 is different for your MongoDB system, update the
port
number. - 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 ofmongodb+srv
. For more information about the differences between the protocols, see Connection String URI Format in the MongoDB documentation.
- Replace [HOSTNAME] with the value found in the
- 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.
- 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 theShort SRV connection string
that you obtained earlier. - Replace DBPASS with the
PASSWORD
value found in theShort SRV connection string
that you obtained earlier.
- Replace DBUSER with the
Procedure
To create the integration server and configure it to connect to MongoDB, complete the following steps:
- From your App Connect Dashboard instance, click the Dashboard icon in the navigation pane.
- On the Servers page, click Create server.
- Choose Quick start integration as the type of integration and click Next.
- Provide CustomerMongoDBV1.bar as the BAR file that you want to deploy and click Next.
- From the Configuration view, use the Create
configuration button to create each of the following configurations in turn:
- 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
. - Select setdbparms.txt as the type and upload the
mongodbsetdbparms.txt file. Give the configuration a meaningful name; for
example,
mongodbsetdbparms
.
- 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,
- Ensure that all of the new configurations that you created are selected and then click Next.
- Define the following details for the integration server:
- Give the integration server a meaningful name; for example,
mongodbtest
. - Ensure that the Designer flows mode is set to
disabled
, which is required when deploying a Toolkit integration. - Set the application transport for the integration endpoint to
http
. - 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.)
- Give the integration server a meaningful name; for example,
Finally, test your flow
To test your flow, complete the following steps:
- Locate the URLs of the REST API that is exposed by the integration server:
- Click the tile for the integration server in the dashboard.
- Click the tile for the application within the server.
- 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. - 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.
- 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.