Skip to main content

Technical

Learning How to Use the MuleSoft MongoDB Connector

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.

Mongo 1

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:

Mongo 2

Click on “create a new project” and name your project:

Mongo 3

Once a project is created, create a database:

Mongo 4

Click on “build database” and choose the shared cluster for your free trial account with your choice of cloud provider and region:

Mongo 5

Then your cluster will be created:

Mongo 6

After the cluster is created, click on “connect to create a DB user detail” and make sure to choose “allow connection from anywhere:”

Mongo 7

Then click on “create a DB user:”

Mongo 8

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.

Mongo 9

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

Mongo 10

Test the connectivity. It is successful if the correct connection string is provided:

Mongo 11

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: Mongo 14

If we need to insert multiple records into our collection:

Mongo 12

Sample input.csv:

Mongo 13

Triggering the flow:

Mongo 15

Data imported from CSV to MongoDB:

Mongo 16

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.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Wubalem Derbe

Wubalem Derbe is a Senior Technical Consultant at Perficient. She has Hands-on experience in developing Mule applications and integrating Service Oriented Architecture (SOA) and Enterprise Application Integration (EAI) using Mule runtimes.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram