CRUD Operations With MongoDB Using Mulesoft

In this article, I will explain how to implement CRUD operations in Mule 4 using the MongoDB Connector.

Prerequisites:

  • For the basics of MongoDB and terminology, please follow this link.
  • For the installation of MongoDB on your local machine, please download the MSI file.
  • For connecting to MongoDB using GUI, please download the MongoDB Compass and extract and launch the MongoDBCompass.exe file.

Introduction

We are going to work on the following operations with the MongoDB connector. Here, we are performing operations on a single record.

  1. Create record/document — We need to use the insert document operation.
  2. Get record/document — For retrieving the record/document from MongoDB, we need to use the find documents operation.
  3. Upsert record/document — For creating/updating records or documents in MongoDB, we need to use the update documents operation.
  4. Delete record/document — We need to use the remove documents operation.

MongoDB Configuration

We need to configure the host, port, database name, username, and password, and we need to add the required dependency for the MongoDB connector.

screenshot of MongoDB config

Practice session on creating a document

post operation

Sample flow for inserting document

In the flow, we are going to create the document in MongoDB. However, before doing this, we need to check whether a document has existed or not. If the record shows it has not existed, we will insert the document or else we are throwing messages if duplicate documents exist.

Configuration XML for creating document:

” data-lang=””>

<flow name="post-operation" doc:id="0be0fbf4-fea2-4d07-9f8e-52bdf440098e" >

        <logger level="INFO" doc:name="Logger" doc:id="090be9c7-d05a-4576-890c-5ee9d33e668f" message="Request received for post operation"/>

        <ee:transform doc:name="Storing empName in variable" doc:id="3d82ac51-e012-4858-875a-1d68c60067b0">

            <ee:message>

            </ee:message>

            <ee:variables >

                <ee:set-variable variableName="empName" ><![CDATA[payload.Name]]></ee:set-variable>

                <ee:set-variable variableName="empId" ><![CDATA[payload.Id]]></ee:set-variable>

                <ee:set-variable variableName="transformedPayload" ><![CDATA[%dw 2.0

output application/json

---

{

     empId:payload.Id,

    empName:payload.Name,

    companyName:payload.Company,

    designation:payload.Designation,

    lastUpdateDateTime: now() as String {format: "dd-MM-yyyy hh:mm:ss" }

}

]]></ee:set-variable>

            </ee:variables>

        </ee:transform>

        <logger level="INFO" doc:name="Logging payload" doc:id="898de03a-5057-461f-921c-02ed15d2f52b" message="Before sending to mongo DB::#[payload]" />

        <ee:transform doc:name="Storing MongoDBQuery" doc:id="ca28111a-c5b3-4df3-8449-a02828dd1343" >

            <ee:message >

            </ee:message>

            <ee:variables >

                <ee:set-variable variableName="mongodbQuery" ><![CDATA[%dw 2.0

output application/json

---

{

  "empId":vars.empId

}]]></ee:set-variable>

            </ee:variables>

        </ee:transform>

        <mongo:find-documents doc:name="Find documents" doc:id="94c3d04e-be8a-4226-8567-7740aa53518f" config-ref="MongoDB_Config" collectionName="test-employee" fields="#[""]">

            <mongo:query ><![CDATA[#[vars.mongodbQuery]]]></mongo:query>

        </mongo:find-documents>

        <ee:transform doc:name="Transform Message" doc:id="58fd172c-757b-43b7-bd14-8d8c11398f63" >

            <ee:message >

                <ee:set-payload ><![CDATA[%dw 2.0

output application/json

---

payload]]></ee:set-payload>

            </ee:message>

        </ee:transform>

        <choice doc:name="Choice" doc:id="d834e7ce-d6bf-483b-88b8-3b1c03a6d75d" >

            <when expression='#[payload != [ ]]'>

                <logger level="INFO" doc:name="Logger" doc:id="ef10fad0-de4c-45df-a9e2-690ebba0263c" message="#["Received duplicate value "]"/>

                <ee:transform doc:name="Transform Message" doc:id="1ab3d771-35df-40ea-8170-1d4d9620051f" >

                    <ee:message >

                        <ee:set-payload ><![CDATA[%dw 2.0

output application/json

---

{ 

    "message":"Duplicate record found"

}]]></ee:set-payload>

                    </ee:message>

                    <ee:variables >

                        <ee:set-variable variableName="httpStatus" ><![CDATA[200]]></ee:set-variable>

                    </ee:variables>

                </ee:transform>

            </when>

            <otherwise >

                <mongo:insert-document doc:name="Insert document" doc:id="bef903a3-68a0-4c29-bcf2-6091356e648c" config-ref="MongoDB_Config" collectionName="test-employee" >

                    <mongo:document ><![CDATA[#[vars.transformedPayload]]]></mongo:document>

                </mongo:insert-document>

                <ee:transform doc:name="Transform Message" doc:id="db882a31-e2af-4ec6-b71a-2e696d44489a">

            <ee:message>

                <ee:set-payload><![CDATA[%dw 2.0

output application/json

---

{

    "message": "Emp Name:" ++ vars.empName   ++ " " ++ "created succesfully in MongoDB"

}]]></ee:set-payload>

            </ee:message>

        </ee:transform>

                <logger level="INFO" doc:name="Logger" doc:id="c94c6f62-a5fe-405c-a2a6-a346a416a41f" message="#[vars.empName] is created successfully and flowname completed" />

            </otherwise>

        </choice>

    </flow>

Retrieving a Document

retrieving a doc

Sample flow for retrieving documents

To read data from a DB based on empId as a query Parameter, create a query in the transform message using the empId query parameter as shown in the below configuration file.

Configuration XML for Retrieving documents:

{ empId: item.empId, empName: item.empName, companyName: item.companyName, “designation”: item.designation, “lastUpdateDateTime”: item.lastUpdateDateTime } )]]>

” data-lang=””>

<flow name="get-operation" doc:id="27893ccf-340c-4847-bbcd-e0984db67eea" >

            <logger level="INFO" doc:name="Start trace" doc:id="32f15888-5cf7-404f-9ba1-dd1fecd9e09d" message="Request received for get method" />

            <ee:transform doc:name="Storing query parameters" doc:id="e9afc5f1-d038-4fb8-90d9-bb6f954aa999" >

                <ee:message >

                </ee:message>

                <ee:variables >

                    <ee:set-variable variableName="empId" ><![CDATA[attributes.queryParams.empId]]></ee:set-variable>

                </ee:variables>

            </ee:transform>

            <ee:transform doc:name="MongoDB query" doc:id="641a8447-94fc-472f-8ddc-b2c1f2a72f4f" >

                <ee:message >

                </ee:message>

                <ee:variables >

                    <ee:set-variable variableName="mongoQuery" ><![CDATA[%dw 2.0

    output application/json

    ---

    {

      "empId":vars.empId

    }]]></ee:set-variable>

                </ee:variables>

            </ee:transform>

            <mongo:find-documents doc:name="Find documents" doc:id="8734d30d-2386-487d-b6dc-41a5cc94e697" config-ref="MongoDB_Config" collectionName="test-employee" fields="#[""]">

                <mongo:query ><![CDATA[#[vars.mongoQuery]]]></mongo:query>

            </mongo:find-documents>

            <ee:transform doc:name="required format" doc:id="b39862ea-3c92-41d7-9460-de1fcbdd9cbb" >

                <ee:message >

                    <ee:set-payload ><![CDATA[%dw 2.0

    output application/json

    ---

    payload map ((item, index) ->

    {

        empId: item.empId,

        empName: item.empName,

        companyName: item.companyName,

        "designation": item.designation,

        "lastUpdateDateTime": item.lastUpdateDateTime

    } )]]></ee:set-payload>

                </ee:message>

            </ee:transform>

            <logger level="INFO" doc:name="End trace" doc:id="4901bebc-5248-4af7-a534-9bdeb11e62bd" message="#[vars.empId] retrieved succesfully and #[flow.name] ended"/>

        </flow>

Submit a Document

Inserting a doc

Sample flow for upserting documents

For updating documents, use a unique parameter like empId to find the documents that we are going to update with the data provided in the request body. If you want to perform the upsert operation, we need to set upsert to true in the advanced settings in the update documents operation.

Configuration XML for upsert document:

<flow name="upsert_operation" doc:id="d8ee3bec-9163-4df9-820c-9bb4acf82f7b" >

        <logger level="INFO" doc:name="Start trace" doc:id="72c3f552-f747-4705-9a5d-93d8da2457c9" message="Request received for upsert operation"/>

        <ee:transform doc:name="required format" doc:id="8964f5a7-1db8-430e-8539-ceb653ba5093" >

            <ee:message >

                <ee:set-payload ><![CDATA[%dw 2.0

output application/json

---

{

    empId:payload.Id,

    empName:payload.Name,

    companyName:payload.Company,

    designation:payload.Designation,

    lastUpdateDateTime: now() as String {format: "dd-MM-yyyy hh:mm:ss" }

}

    

    

    

]]></ee:set-payload>

            </ee:message>

            <ee:variables >

                <ee:set-variable variableName="empId" ><![CDATA[payload.Id]]></ee:set-variable>

                <ee:set-variable variableName="empName" ><![CDATA[payload.Name]]></ee:set-variable>

            </ee:variables>

        </ee:transform>

        <logger level="INFO" doc:name="Logger" doc:id="30cadc90-fb84-40a2-bf6a-b3d13ccf3d64" message="Payload Before MongoDB operation::::#[payload]"/>

        <ee:transform doc:name="mongoDB query" doc:id="70c70caa-8756-4b92-8758-c609ce709a39" >

            <ee:message >

            </ee:message>

            <ee:variables >

                <ee:set-variable variableName="mongoDB" ><![CDATA[%dw 2.0

output application/json

---

{

  "empId":vars.empId

}]]></ee:set-variable>

            </ee:variables>

        </ee:transform>

        <mongo:update-documents doc:name="Update documents" doc:id="4c6713f9-9e7d-4da2-9325-a5b1b65d555b" config-ref="MongoDB_Config" collectionName="test-employee" upsert="true">

            <mongo:query ><![CDATA[#[vars.mongoDB]]]></mongo:query>

        </mongo:update-documents>

        <choice doc:name="Choice" doc:id="2e1f89df-051f-497a-9cb0-c9a78cdef397" >

            <when expression='payload.upsertedId == null'>

                <ee:transform doc:name="Transform Message" doc:id="cff398c1-9eaf-4726-a418-5f5330513d48">

            <ee:message>

                <ee:set-payload><![CDATA[%dw 2.0

output application/json

---

{

    "message": "Emp Name:" ++ vars.empName   ++ " " ++ "updated succesfully in MongoDB"

}]]></ee:set-payload>

            </ee:message>

                    <ee:variables>

                        <ee:set-variable variableName="httpStatus"><![CDATA[200]]></ee:set-variable>

                    </ee:variables>

        </ee:transform>

            </when>

            <otherwise>

                <ee:transform doc:name="Transform Message" doc:id="ed24ed63-559d-4f73-94cb-236e9248e689">

                    <ee:message>

                        <ee:set-payload><![CDATA[%dw 2.0

output application/json

---

{

    "message": "Emp Name:" ++ vars.empName   ++ "  " ++ "created  succesfully in MongoDB"

}]]></ee:set-payload>

                    </ee:message>

                    <ee:variables>

                        <ee:set-variable variableName="httpStatus"><![CDATA[201]]></ee:set-variable>

                    </ee:variables>

                </ee:transform>

            </otherwise>

        </choice>

        <logger level="INFO" doc:name="Endtrace" doc:id="8d22b417-7dde-4105-aa11-1bf9be6004c2" message="upsert operation completed and #[flow.name] completed"/>

    </flow>

Delete a Document

deleting a document

Sample flow for deleting documents

For deleting data from DB based on empId as query Parameter, create a delete query in the transform message using empId query parameter as shown in the below configuration file.

Configuration XML for delete document:

<flow name="delete-operation" doc:id="94fac13e-b531-4d6f-9571-f59614b5f24b" >

        <logger level="INFO" doc:name="Logger" doc:id="5c9e896a-5824-4f45-b4ad-e70731ca7ce7" message="Delete flow started"/>

        <ee:transform doc:name="Storing empId in a variable" doc:id="1f0e9e10-3784-406d-8c13-1f57c00e200d" >

            <ee:message >

            </ee:message>

            <ee:variables >

                <ee:set-variable variableName="empId" ><![CDATA[attributes.queryParams.empId]]></ee:set-variable>

            </ee:variables>

        </ee:transform>

        <ee:transform doc:name="Mongodb Query" doc:id="018875d2-5c33-4e19-a828-0125cf2331af" >

            <ee:message >

            </ee:message>

            <ee:variables >

                <ee:set-variable variableName="mongodbQuery" ><![CDATA[%dw 2.0

output application/json

---

{

  "empId":vars.empId

}]]></ee:set-variable>

            </ee:variables>

        </ee:transform>

        <mongo:remove-documents doc:name="Remove documents" doc:id="0f333a9a-04ff-450e-a230-083a53496adc" config-ref="MongoDB_Config" collectionName="test-employee">

            <mongo:query><![CDATA[#[vars.mongodbQuery]]]></mongo:query>

        </mongo:remove-documents>

        <ee:transform doc:name="Transform Message" doc:id="8fce3ae6-9d92-450f-b572-75154279cb57">

            <ee:message>

                <ee:set-payload><![CDATA[%dw 2.0

output application/json

---

{

    message: if(payload == 1) ("empId:" ++ vars.empId ++ " " ++ "deleted succesfully from MongoDB") else "Record not found" 

    

}]]></ee:set-payload>

            </ee:message>

        </ee:transform>

        <logger level="INFO" doc:name="Logger" doc:id="7c2d4c97-54f6-48d4-9e4f-9a270758b79c" message="#[payload]"/>

    </flow>

.

Leave a Comment