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.
Join the DZone community and get the full member experience.
Join For FreeMongoDB 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.
MongoDB Setup.
Configure the MongoDB component in Mule Flow.
Execute the flow to save a document.
Execute the flow to retrieve the document saved.
Set Up
Install the MongoDB that your OS supports.
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\dbStart 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:
Configure the MongoDB component as below.
Provide username, password, database name, and HOST & PORT details.
Configure the properties of the MongoDB component as follows.
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.
Execute the Flow to Retrieve the Saved Document
To query MongoDB for documents, use the "Find One Document" operation. The flow is as follows:
The MongoDB configuration details for the retrieval of the document are as follows:
Query MongoDB through the Postman client as below; the query hits the MongoDB component in the Mule flow:
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.
The MongoDB connector has the capability to store and retrieve documents from the Mule flow efficiently and with minimal configuration.
Opinions expressed by DZone contributors are their own.
Comments