Contact Us 1-800-596-4880

MongoDB Connector 6.2 Examples - Mule 4

Invoke an Operation

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

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

  2. From the palette, select MongoDB and drag the Insert Document operation into your flow by placing it to the right of HTTP Listener.

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

  4. From the palette, drag the Transform Message component to the left of MongoDB Connector.

  5. Click Transform Message and enter two key-value pairs:

    %dw 2.0
    %output application/json
    ---
    {
    	name:"Peter",
    	age:"42"
    }
  6. From the Mule Palette view, drag other Transform Message components to the right of MongoDB Connector.

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

    %dw 2.0
    output application/json
    ---
    payload

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/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/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 database="${database}">
			<mongo:server-addresses >
				<mongo:server-address host="${host}" port="${port}"/>
			</mongo:server-addresses>
		</mongo:connection>
    </mongo:config>

    <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
---
{
	name:"Pity",
	age:"31"
}]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <mongo:insert-document config-ref="MongoDB_Config"
               doc:name="Insert document"
               collectionName="${collection}" />
        <logger level="INFO" doc:name="Logger"
	  message="New document:  #[payload]" />
    </flow>
</mule>
View on GitHub