Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Accessing MongoDB in Mule Flow

DZone's Guide to

Accessing MongoDB in Mule Flow

In this walkthrough, learn the steps to set up MongoDB to quickly store and retrieve documents from the database through Mule Flow.

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

MongoDB is leading open source NoSQL database. In this post, I am going to execute the basic operations to store and retrieve the documents from MongoDB through Mule Flow.

  1. MongoDB Setup.

  2. Configure the MongoDB component in Mule Flow.

  3. Execute the flow to save a document.

  4. Execute the flow to retrieve the document saved.

Set Up

  1. Install the MongoDB that your OS supports.

  2. MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is c:\data\db. So you need to create this folder using the Command Prompt.
            Create the DB using the commands
            C:\>md data
             C:\md data\db

  3. Start the MongoDB server:
    Open new DOS Command prompt as Administrator and Navigate to Mongo DB installation folder: C:\Program Files\MongoDB\Server\3.4\bin > mongod

Configure the MongoDB Component in Mule Flow

1) Add the HTTP Connector and configure the endpoint by providing HOST and PORT details.

2) Add MongoDB component to the flow next to HTTP connector.

3) Optionally, add a logger component to watch the input:

Image title

Configure the MongoDB component as below. 

Provide username, password, database name, and HOST & PORT details.

Image title

Configure the properties of the MongoDB component as follows.

Image title

Now we need to provide the collection name to store the data (here, "developer").

Insert the New Document in the MongoDB Collection

Post the document via an HTTP Post request to the Mule flow through Postman.

Image title

Execute the Flow to Retrieve the Saved Document

To query MongoDB for documents, use the "Find One Document" operation. The flow is as follows:

Image title

The MongoDB configuration details for the retrieval of the document are as follows:

Image title

Query MongoDB through the Postman client as below; the query hits the MongoDB component in the Mule flow:

Image title

The document has been successfully retrieved from MongoDB. Observe the document retrieved through the log statement of the logger component. MongoDB generates a unique ID for each document.

Image title

The MongoDB connector has the capability to store and retrieve documents from the Mule flow efficiently and with minimal configuration. 

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
mule esb ,mongodb ,integration ,mule flow

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}