Nav

MongoDB Connector

Select

MongoDB Connector is a closed source connector which provides a connection between Mule and third-party software MongoDB server.

Prerequisites

This document assumes that you are familiar with MongoDB API. To use the this connector you need the following:

  • MongoDB instance.

  • Set of valid credentials, including the required MongoDB endpoints, pointing to your instance.

  • MuleSoft Enterprise License.

What’s New in this Connector

  • Support for Mule 4.

  • The connector now provides metadata for collections, files, and documents.

To Connect in Design Center

  1. In Design Center, click Set Up > Upload, browse for and select the driver for this connector on your file system, and upload it. Alternatively, search for and select a driver that is already uploaded.

  2. Click a trigger. You can create a global element by selecting this connector as they trigger. If a global element is not needed, you can use an HTTP Listener or Scheduler trigger.

  3. To create an optional global element for the connector, set these fields:

    1. Select the plus sign to add a component.

    2. Select the connector as a component.

    3. Select an operation.

    4. Set up a MongoDB configuration:

      Fields Description

      Servers

      This location points to the default MongoDB instance.

      Username

      MongoDB username.

      Password

      MongoDB password.

      Database

      MongoDB database.

      Enable Metadata

      Enables metadata for collections, documents and files.

      Collection metadata samples

      Allows you to add existing collections to take metadata structure from.

To make a simple flow, you can select these operations:

  • Insert Document.

    Fields Description

    Collection Name

    This location points to the collection you want. This collection needs to be created in the database which you have selected before.

  • Find one and Update Document.

  • Remove Document.

Connect in Anypoint Studio 7

Configure in Studio

You need to put the dependency in your file pom.xml.


          
       
1
2
3
4
5
<dependency>
  <groupId>org.mule.connectors</groupId>
  <artifactId>mule-mongo-connector</artifactId>
  <version>5.0.0</version>
</dependency>

To Set Up the Global Configuration

  1. Go to the Global Element tab and click Create.

  2. Search for MongoDB from the Connector Configuration directory.

  3. When you select the global element of your choice, you are presented with the global element properties to enter.

    1. Select the type of Connection

    2. The Username field refers to the username you use to access your MongoDB instance.

    3. The Password field should contain the corresponding password.

    4. You also need to specify the Database used to send and retrieve data from your MongoDB instance. Before running your application, make sure that the user has the necessary permissions to do so.

    5. You may also need to set the Servers property which is composed of a host:port comma-separated list (in case you have a replica set). This attribute is optional, and if left empty, it defaults to localhost:27017.

    6. In the Connection tab, you may adjust the values for the Connect Timeout, Connections Per Host, Max Wait Time and the Socket Timeout for requests that are sent to MongoDB. The default value for Connect Timeout is 30000 ms. This means that any connection requests that take longer than 30 seconds to be sent and received throws an exception. You can override this behavior by setting their values to 0, meaning that the MongoDB connector waits indefinitely until a request is successfully sent and a response is received.

    7. You may set if the metadata is enabled or not, and add a collection sample.

The following shows the MongoDB configuration with Spring property placeholders for the Connection field values. This is the recommended approach when you plan to deploy your application to CloudHub or to a Mule ESB server. However, you may harcode your connection credentials in the Global Element Properties shown below if you are in the development stage and simply want to speed up the process.

Fields Description

Servers

This location points to the default MongoDB instance.

Username

MongoDB username.

Password

MongoDB password.

Database

MongoDB database.

Enable Metadata

Enables metadata for collections, documents and files.

Collection metadata samples

Allows you to add existing collections to take metadata structure from.

Setting up the Global Configuration with SSL (Optional)

As of MongoDB Connector v4.0.0, we support the use of SSL. To setup the Global Configuration:

  • In the Global Element tab for your MongoDB connection, make sure you select "true" SSL property field in the Connection tab.

  • Remember, your instance must support this feature.

Use Case: Studio

To invoke a simple operation (such as the Insert Document operation), you can follow these steps:

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

  2. 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

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

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

    
                
             
    1
    2
    3
    4
    5
    6
    7
    
    %dw 2.0
    %output application/json
    ---
    {
            name:"Peter",
            age:"42"
    }
  5. From the palette, drag and drop other Transform Message component after the MongoDB connector.

  6. Click on the new Transform Message and set the output to application/json.

    
                
             
    1
    2
    3
    4
    
    %dw 2.0
    output application/json
    ---
    payload
  7. Other possible flow options are: Find One Document, Remove Documents, or Update Document.

    1. Update Document.

    2. Remove Documents.

    3. Find One Document:

Use Case: XML


         
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?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" doc:id="08508a01-02c3-435a-a17a-b9149204cdfb">
        <http:listener-connection host="0.0.0.0"
                                  port="8081" />
    </http:listener-config>
    <mongo:config name="MongoDB_Config" doc:name="MongoDB Config"
                  doc:id="7de194db-4d35-4fe4-819b-2687710c2a39">
        <mongo:connection username="${username}" password="${password}"
                          database="${database}" host="${host}" />
    </mongo:config>
    <flow name="html-form-flow" doc:id="4512071a-09a9-4364-a47e-7ddc9632da6a">
        <http:listener config-ref="HTTP_Listener_config" path="/"
                       doc:name="Listener" doc:id="4ca0db5c-7bd8-48eb-8b72-fdd15bf44d23" />
        <parse-template location="form.html" doc:name="Parse Template"
                        doc:id="a58bf0d5-525e-4127-95eb-6566ffe111a6" />
    </flow>
    <flow name="create-mongo-record-flow" doc:id="e9fd320c-912c-4063-9cf5-354c2c34a82d">
        <http:listener config-ref="HTTP_Listener_config" path="/create"
                       doc:name="Listener" doc:id="a119b648-2721-46fc-9c4a-12126ee2d806" />
        <ee:transform doc:name="Set Payload and Document Content"
                      doc:id="8c3f3ee7-e3d9-4682-af4d-63af3fdab88f">
            <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"
                doc:id="2e9de838-21b9-4b18-a7e3-db8f6351e9a5"
                message="#['Creating record in Mongo. Message will be: ' ++ payload.content]" />
        <mongo:insert-document config-ref="MongoDB_Config"
                               doc:name="Insert document" doc:id="438f9ae2-f823-442e-8154-1d09e407a600"
                               collectionName="${collection}" />
        <logger level="INFO" doc:name="Logger" doc:id="0ef68faf-da4c-4be2-baba-34a5c0f370e5" message="#['ID of the new document: ' ++ payload]" />
        <ee:transform doc:name="Set Payload"
                      doc:id="34f0f8aa-f67c-4005-a492-dd8286ecae66">
            <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" doc:id="a8725a0b-d3bc-4c4e-8f5f-15089a923a05">
        <http:listener config-ref="HTTP_Listener_config" path="/read"
                       doc:name="Listener" doc:id="c2445e45-a15c-4990-8c9f-a2ac09829556" />
        <ee:transform doc:name="Set Payload"
                      doc:id="edc7f4ec-9bc2-4903-82fd-bff55a30a667">
            <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" doc:id="a076ee0c-fb81-4154-bc07-3dc0d60fbdeb" 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" doc:id="fe82172b-2ce0-461e-b196-50e1186cd247" />
        <ee:transform doc:name="Set Payload for Document ID" doc:id="19d25266-1e2b-4ece-883a-6c4683f70148" >
            <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" doc:id="e0ee28cd-dfb1-49db-8dfc-30c1dcf01d63" message="#['ID of the found document: ' ++ payload]"/>
    </flow>
    <flow name="update-mongo-record-flow" doc:id="3e2ae866-c481-42b8-9c09-ae4d22ba911b" >
        <http:listener config-ref="HTTP_Listener_config" path="/update" doc:name="Listener" doc:id="17dc917b-bb4b-4db7-906b-83ca3e9b569e" />
        <ee:transform doc:name="Set payload and query" doc:id="22b3e519-b3a7-492f-8329-9defdcd9d3cf" >
            <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" doc:id="5957010f-4661-4c00-913e-007923cadae0" message="#['New message: ' ++ payload.content]" />
        <mongo:update-documents config-ref="MongoDB_Config" collectionName="${collection}" doc:name="Update documents" doc:id="7c36057c-eb91-437d-9eae-00aa50ea412b">
            <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" doc:id="e8f85634-a266-4f3a-ba4d-50545efea6b7" >
            <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" doc:id="2b50ab1d-68da-4c4b-91df-5193c9b51668" >
        <http:listener config-ref="HTTP_Listener_config" path="/delete" doc:name="Listener" doc:id="71f27d06-91f1-4b0f-876f-44bed8b31853" />
        <ee:transform doc:name="Set Payload" doc:id="cd4f10f6-d320-4da4-8b19-b75c0197d81a" >
            <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" doc:id="99dc481b-ab95-47c2-a905-f6fa0bda0fbc" message="#['Deleting entry from Mongo with ID : ' ++ payload[0][0]]" />
        <mongo:remove-documents config-ref="MongoDB_Config" collectionName="${collection}" doc:name="Remove documents" doc:id="86b9603b-a463-45ef-ace8-6f68c2c3aaaf" />
        <ee:transform doc:name="Set message for delete document" doc:id="5d298c93-2129-4216-9667-08e937315f07" >
            <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" doc:id="e4063c9d-f120-47a9-a90d-79d7a2a233aa" message="#[payload]"/>
    </flow>
</mule>