Connecting to MongoDB from ESB Mule
Tags: mongodb, anypoint, esb
Published: 2021-10-09 19:59:48


I recently discovered Anypoint Studio and the first question that came up to me was: how can I access my MongoDB cluster?
This is the fastest solution I have found :)

## Create the Mule Project

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbbef2lvc0hqxo0446at.png)

---

## Configure an HTTP Listener 

With the sole purpose of being able to test our project later, I have added and configured an HTTP Listener:

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbjkb4v6nydd36eghelf.png)

## Install and configure the MongoDB connector

1. Open the Mule Palette view and select `Search in Exchange`.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlsjkwnmsagyz9rs9l0e.png)
2. In the search box type "mongo" and search "MongoDB Connector" in the Available Modules tab. Then select it and click `add` and `finish`.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z43p1ysgha8p1pvnst71.png)

     > **Make sure you choose the 6.3 version or higher.**
 

3. Navigate to `Global Elements` tab.
4. Select `Create` > `Connector Configuration` > MongoDB config
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vx0w4tuvdimh6bq4793c.png)
 

5. In the `General` tab select "Connection string" as connection option.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4khoykehn369fa1aelj.png)
6. Some libraries must be installed. Just click `Configure...` > `Add Recommended Libraries`
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26hmug5ufbrhw02o7tcb.png)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11noxqdgzn4ny883l2cp.png)

Now we must include the connection string from our MongoDB atlas clusters.

## Get your Connection String

1. Go to your MongoDB Atlas project
2. Select connect
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uggzayu9thrabcvrx1oz.png)

3. Choose "Connect your application" and select `Java` as your application language (this is just for getting the connection string).
4. Then copy the connection string and follow the MongoDB instructions (_Replace password..._)
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7czt8xdp43n8ky0retn.png)

5. Paste it to the Mule Connection string textbox...
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pxans8bclp2rc4evvl9.png)
 
6. Select `Test Connection` to test if alll is working well
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtw4789vo7yveaeuy9cv.png)

 **[If you are having issues check the project allowed IPs and the database allowed users in your MongoDB settings]**


---

## Try it!

1. From the Mule pallete add `Create Collection` and `Insert document` elements and select as Connector_Configuration the config we have just created.

2. Configure both elements with the collection name and the document you want to insert...
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9l8flk3mvd26g40rnfs.png)

3. Run the project, go to the HTTP listener url and then to your MongoDB collection list...
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3fwonp0gkxuoemwnlypz.png)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zvctorr09kdwbgg9dqle.png)


And that's all!  I hope this information is useful for your projects :)