MuleSoft has various pre-built connectors to connect Mule to different databases and third-party software. The Anypoint Connector for MongoDB (MongoDB Connector) is a closed-source connector that provides a connection between the Mule runtime engine and third-party software on a MongoDB server.
MongoDB is similar to the MuleSoft Database Connector, which uses a single interface for every case and allows developers to run SQL operations on their database. By leveraging the MongoDB Connector, we can import, export, backup, analyze, transform, and connect to MongoDB. This connector provides the easiest way to connect with MongoDB from the MuleSoft workflow by utilizing a connection string or connectivity details in the MongoDB Connector.
Below are supported operations within the MongoDB Connector, as shown in the screenshot from the Anypoint Studio Mule palette. I will talk about some of the operations later in a use case discussion.
Getting Started With the MongoDB Connector
To familiarize yourself with the MongoDB Connector, you can sign up for a free trial, cloud-hosted MongoDB service.
Create a free trial account below from Atlas with the following steps:
- Go to https://account.mongodb.com/
- Sign up for a trial account or sign in if you already have one
- After verifying your email, you will be prompted to log in and taken to the below page:
Click on “create a new project” and name your project:
Once a project is created, create a database:
Click on “build database” and choose the shared cluster for your free trial account with your choice of cloud provider and region:
Then your cluster will be created:
After the cluster is created, click on “connect to create a DB user detail” and make sure to choose “allow connection from anywhere:”
Then click on “create a DB user:”
Next, choose the connection method using Native app and select “Java as a driver” from the drop-down. From here, you will get the connection string to be used in MuleSoft to connect to Mongo DB.
Configuring Mongo DB Connector in AnyPoint Studio
In the global config for MongoDB, select “connection string” from the connection drop-down. Then select the “recommended library” option as in the below screenshot for a MongoDB driver. Finally, use the connection string by updating it with your details (username, password, and the collection name to be created) in the above step to connect to MongoDB.
For example: mongodb+srv://admin1:admin1@cluster0.ahzus.mongodb.net/customers?retryWrites=true&w=majority
Test the connectivity. It is successful if the correct connection string is provided:
Now that we’ve walked through how to get started with the MongoDB Connector and configure it in Anypoint Studio, let’s take a look at a use case.
Use Case: Importing Data from CSV to MongoDB
In this use case, we are polling a CSV file from one location using a file connector and then transforming it as per the mapping to be stored in MongoDB.
First, we check to see if the collection where we need to store data already exists or not. If the collection isn’t present within MongoDB, we create the collection first. If the collection is present, we store the transformed data using Insert Documents.
To check if the collection doesn’t exist:
If we need to insert multiple records into our collection:
Sample input.csv:
Triggering the flow:
Data imported from CSV to MongoDB:
To learn more about the MongoDB Connector, click here.
Perficient + MuleSoft
At Perficient, we excel in tactical MuleSoft implementations by helping you address the full spectrum of challenges with lasting solutions, rather than relying on band-aid fixes. The end result is an intelligent, multifunctional resource that reduces the costs over time and equips your organization to proactively prepare for future integration demands.
We’re a Premier MuleSoft partner with more than 15 years of integration expertise across industries including financial services, healthcare, retail, and more. After MuleSoft’s acquisition by Salesforce, our continued innovation in the integration space offers more customized experiences on software developed by MuleSoft. We combine the MuleSoft product suite with our connectivity expertise to provide comprehensive solutions both on-premises and in the cloud.
Contact us today to learn how we can help you implement MuleSoft to solve your enterprise’s integration challenges.