DEV Community

Cover image for Connecting to MongoDB from ESB Mule
David Jiménez
David Jiménez

Posted on

Connecting to MongoDB from ESB Mule

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


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

Install and configure the MongoDB connector

  1. Open the Mule Palette view and select Search in Exchange. image
  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

    Make sure you choose the 6.3 version or higher.

  3. Navigate to Global Elements tab.

  4. Select Create > Connector Configuration > MongoDB config
    image

  5. In the General tab select "Connection string" as connection option.
    image

  6. Some libraries must be installed. Just click Configure... > Add Recommended Libraries
    image
    image

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

  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

  5. Paste it to the Mule Connection string textbox...
    image

  6. Select Test Connection to test if alll is working well
    image

[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

  3. Run the project, go to the HTTP listener url and then to your MongoDB collection list...
    image

image

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

Top comments (0)