Contact Free trial Login

MongoDB Examples - Mule 4

Invoke an Operation

To invoke a simple operation, such as the Insert Document operation:

  1. In Studio, click HTTP and drag the Listener operation to the canvas.

  2. From the palette, select MongoDB, and drag and drop the Insert Document operation into your flow by placing it after the HTTP listener.

  3. Configure the connector by selecting the Connector Configuration you created in the previous section and choosing the operation to invoke and set the Collection name.

  4. From the palette, drag and drop the Transform Message component before the MongoDB connector.

  5. Click on Transform Message and enter two key-value pairs as shown below.

    %dw 2.0
    %output application/json
    ---
    {
    	name:"Peter",
    	age:"42"
    }
  6. From the palette, drag and drop other Transform Message components after the MongoDB connector.

  7. Click the Transform Message and set the output to application/json.

    %dw 2.0
    output application/json
    ---
    payload
  8. Other possible flow options are:

    • Find One Document

    • Remove Documents

    • Update Document

XML for Invoke an Operation

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:mongo="http://www.mulesoft.org/schema/mule/mongo"
	xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	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:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="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/ee/core
	http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
	http://www.mulesoft.org/schema/mule/mongo
	http://www.mulesoft.org/schema/mule/mongo/current/mule-mongo.xsd">

    <configuration-properties file="mule-app.properties" />
    <http:listener-config name="HTTP_Listener_config"
                          doc:name="HTTP Listener config">
        <http:listener-connection host="0.0.0.0"
                                  port="8081" />
    </http:listener-config>
    <mongo:config name="MongoDB_Config" doc:name="MongoDB Config">
        <mongo:connection username="${username}" password="${password}"
                          database="${database}" host="${host}" />
    </mongo:config>
    <flow name="html-form-flow">
        <http:listener config-ref="HTTP_Listener_config" path="/"
                       doc:name="Listener" />
        <parse-template location="form.html" doc:name="Parse Template" />
    </flow>
    <flow name="create-mongo-record-flow" >
        <http:listener config-ref="HTTP_Listener_config" path="/create"
                       doc:name="Listener" />
        <ee:transform doc:name="Set Payload and Document Content">
            <ee:message>
                <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
	"content": payload.message
}]]></ee:set-payload>
            </ee:message>
            <ee:variables>
                <ee:set-variable variableName="content"><![CDATA[%dw 2.0
output application/java
---
payload.message]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <logger level="INFO" doc:name="Logger"
          message="#['Creating record in Mongo. Message is: ' ++ payload.content]" />
        <mongo:insert-document config-ref="MongoDB_Config"
               doc:name="Insert document"
               collectionName="${collection}" />
        <logger level="INFO" doc:name="Logger"
	  message="#['ID of the new document: ' ++ payload]" />
        <ee:transform doc:name="Set Payload">
            <ee:message>
                <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
vars.content]]></ee:set-payload>
            </ee:message>
        </ee:transform>
    </flow>
    <flow name="read-mongo-record-flow" >
        <http:listener config-ref="HTTP_Listener_config" path="/read"
                       doc:name="Listener"  />
        <ee:transform doc:name="Set Payload">
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	content: payload.message
}]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger"
	  message="#['Finding a single record that has content equal to: ' ++ payload.content]" />
        <mongo:find-one-document config-ref="MongoDB_Config"
	collectionName="${collection}"
	doc:name="Find one document" />
        <ee:transform doc:name="Set Payload for Document ID" \>
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload.'_id'.'\$oid'
]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger"
	  message="#['ID of the found document: ' ++ payload]"/>
    </flow>
    <flow name="update-mongo-record-flow" >
        <http:listener config-ref="HTTP_Listener_config" path="/update" doc:name="Listener" />
        <ee:transform doc:name="Set payload and query" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	content: payload.updateValue
}]]></ee:set-payload>
            </ee:message>
            <ee:variables >
                <ee:set-variable variableName="query" ><![CDATA[%dw 2.0
output application/json
---
{
	"_id" : {
        "\$oid": payload.updateObjectId
    }
}]]></ee:set-variable>
            </ee:variables>
        </ee:transform>
        <logger level="INFO" doc:name="Logger"
	  message="#['New message: ' ++ payload.content]" />
        <mongo:update-documents config-ref="MongoDB_Config"
	collectionName="${collection}" doc:name="Update documents" >
            <mongo:find-query ><![CDATA[#[vars.query]]]></mongo:find-query>
            <mongo:content-to-update ><![CDATA[#[payload]]]></mongo:content-to-update>
        </mongo:update-documents>
        <ee:transform doc:name="Set Payload for Document ID" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
vars.query[0][0]]]></ee:set-payload>
            </ee:message>
        </ee:transform>
    </flow>
    <flow name="delete-mongo-record-flow" >
        <http:listener config-ref="HTTP_Listener_config"
	path="/delete" doc:name="Listener" />
        <ee:transform doc:name="Set Payload" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	"_id" : {
        "\$oid": payload.deleteObjectId
    }
}]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger"
	  message="#['Deleting entry from Mongo with ID : ' ++ payload[0][0]]" />
        <mongo:remove-documents config-ref="MongoDB_Config"
	  collectionName="${collection}" doc:name="Remove documents"  />
        <ee:transform doc:name="Set message for delete document" >
            <ee:message >
                <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
"Entry was deleted from MongoDB" as String]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <logger level="INFO" doc:name="Logger"
	 message="#[payload]"/>
    </flow>
</mule>

See Also

We use cookies to make interactions with our websites and services easy and meaningful, to better understand how they are used and to tailor advertising. You can read more and make your cookie choices here. By continuing to use this site you are giving us your consent to do this.