Mulesoft with MongoDB is one of the best combinations for big data processing. MongoDB is leading open source NoSQL database and Mule Soft is leading open source ESB. This blog is dedicated to integration of Mulesoft with MongoDB .
Installation and configuration of MongoDB
Install MongoDB in your system. I am using window installation of mongoDB. I installed mongoDB 3.0 in my C:\ MongoDB folder. I created data folder to store MongoDB data in C:\data.
start MongoDB server with command
> \MongoDB\Server\3.0\bin\mongod.exe" --dbpath \ data
MongoDB server started in port –27017 and userId—admin
Now download test data from MongoDB website
https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/dataset.json
save this to a file named primer-dataset.json.
Import this data into MongoDB with test instance > mongoimport --db test --collection restaurants --drop --file \temp\primer-dataset.json
Start mongo editor to test loaded data > MongoDB\Server\3.0\bin\mongo.exe
Run this command to test your database is configured > db.restaurants.find().count() This will return result.
Configuration of MongoDB connector in Mulesoft
Now configure MongoDB connection in Mule. I am using Mule 3.7
I created small Mule flow to work with MongoDB integration with Mule. This flow getting http request to get data from MongoDB and sending those data in json object.
MongoDB connection configuration screenshot
Now we have restaurants collection in MongoDB , So I use collection as restaurants.To execute condition query I am using operation – Find object using query map
In Query Attributes I am using — Create Object manually
Here is full code of this implementation
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:mongo="http://www.mulesoft.org/schema/mule/mongo" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.7.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/mongo http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" basePath="/mongo" doc:name="HTTP Listener Configuration"/>
<mongo:config name="Mongo_DB" database="test" doc:name="Mongo DB" username="admin"/>
<flow name="mongodbprojectFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/mongoproject" doc:name="HTTP"/>
<logger message="This is Http Request and Response" level="INFO" doc:name="Logger"/>
<mongo:find-objects-using-query-map config-ref="Mongo_DB" doc:name="Mongo DB" collection="restaurants" >
<mongo:query-attributes>
<mongo:query-attribute key="restaurant_id">40360045</mongo:query-attribute>
</mongo:query-attributes>
<mongo:fields>
<mongo:field>name</mongo:field>
<mongo:field>cuisine</mongo:field>
<mongo:field>borough</mongo:field>
</mongo:fields>
</mongo:find-objects-using-query-map>
<mongo:mongo-collection-to-json doc:name="Mongo DB"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>
</mule>
Rajnish Kumar is CTO of Vanrish Technology with Over 25 years experience in different industries and technology. He is very passionate about innovation and latest technology like APIs, IOT (Internet Of Things), Artificial Intelligence (AI) ecosystem and Cybersecurity. He present his idea in different platforms and help customer to their digital transformation journey.