Support Category: Select

MongoDB is a cross-platform document-oriented database. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.

Note: MongoDB Connector v4.1 introduced the Map Reduce feature.

Introduction

MongoDB Connector is a closed source connector which provides a connection between Mule and third-party software MongoDB server. It implements the MongoDB v3.0 API modules and it also includes different types of transformers for data management.

The latest MongoDB Connector is an SDK-based connector developed with DevKit 3.9.0.

Note
The Select versions of the connector are MongoDB connector MongoDB v4 and later. The Community versions of the connector are MongoDB connector v3 and earlier.

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

Dependencies

The MongoDB connector requires the following dependencies:

Application/Service Version

Anypoint Studio

3.7

Mule Runtime

EE 3.6.0 and above

MongoDB

MongoDB Java driver 3.6.0

Java

JDK 7 and above

Install and Configure

Installing

To install the MongoDB Connector:

  1. In Anypoint Studio, click the Exchange icon in the Studio taskbar.

  2. Click Login in Anypoint Exchange.

  3. Search for this connector and click Install.

  4. Follow the prompts to install the connector.

  5. Click Next, Next, and accept the license agreement.

  6. Restart Studio when prompted.

Updating from an Older Version

  1. Uninstall the previous MongoDB Connector.

  2. In Anypoint Studio, click the Exchange icon in the Studio taskbar.

  3. Click Login in Anypoint Exchange.

  4. Search for this connector and click Install.

  5. Follow the prompts to install the connector.

  6. Ensure that the Maven dependencies have been updated correctly from Mongo DB (v3.n.n) to Mongo DB (v4.4.n).

  7. Update flows that are using Phased Out or Deprecated operations.

    1. Most operation names have been changed to comply with Mongo Driver v3, so if you had an InsertObject operation, it now appears as InsertDocument.

    2. WriteResult objects have been removed. Now your operations only return Strings, Documents, and DBObjects.

    3. MongoDB transformers have been replaced by implicit ones. So, if your input data is a Map and, you want to perform an Insert operation, just drag a MongoDB connector into the workspace, choose the InsertDocument operation and you’re done! You no longer require the use of the transformer.

    4. Remember that mongo:config does NOT support the port field anymore. Specify the port inside the host field of your mongo:config using the format "host:port". For more information see the MongoDB Connector Migration Guide.

Use This Connector

For a list of all the operations refer to the latest MongoDB Connector API Reference.

Example Application Setup

  • Start by inserting your MongoDB credentials and endpoints in a .properties file and load them using the Property Placeholder component.

  • Search for and drag the HTTP connector to the Anypoint Studio Canvas

  • Search for and drag the Transform Message component to the Canvas.

  • Search for and drag the MongoDB connector to the Canvas.

    MongoDB Connector initial flow

Set Up the Connector Global Element

To set up the global element:

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

  2. Inside the filter text box type "Mongo", and select 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. The Username field refers to the username you use to access your MongoDB instance.

    2. The Password field contains the corresponding password.

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

    4. You may also need to set the Servers (host:port) 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.

    5. If needed, click the SSL checkbox to access the database over SSL. For more information, see Setting up the Global Configuration with SSL (optional).

    6. 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 30000ms. This means that any connection requests that take longer than 30 seconds to be sent and received throw 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.

The following screenshot shows the MongoDB configuration with Spring property placeholders for the Connection field values. For more information, see Configuring Properties. This is the recommended approach when you plan to deploy your application to Runtime Manager or to a Mule server. However, you may hard code 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.

MongoDB Connector Global Elements
s
Fields Description

Username

MongoDB username. Input your username using Mule property placeholder syntax.

Password

MongoDB password. Input your password using Mule property placeholder syntax.

Database

MongoDB database. Input your database using Mule property placeholder syntax.

Servers

This location points to the default MongoDB instance, Input your endpoint using Mule property placeholder syntax.

Set Up the Global Configuration with SSL (optional)

MongoDB Connector v4.0.0 and later supports the use of SSL.

To set up the Global Configuration:

  • In the Global Element tab for your MongoDB connection, check the SSL property field.

  • Remember, your instance must support this feature. For more information check MongoDB Configure SSL documentation.

Invoke an Operation

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

  1. Locate, and drag and drop the HTTP connector, Transform Message, and MongoDB connector onto the Anypoint Studio Canvas.

  2. Configure the MongoDB connector by selecting the Connector Configuration you created in the previous section and choosing the operation to invoke.

    MongoDB Connector
  3. Click Transform Message and enter these two key-value pairs:

    %dw 1.0
    %output application/java
    ---
    {
    	name:"Peter",
    	age:"42"
    }
    Transform Message

Example XML Flow

The following example provides a DataWeave transform and the use of the Mongo DB connector.

Note
MongoDB v4 does an automatic conversion for "Document to JSON" and "JSON to Document".
<flow name="insert-document-flow">
  <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
  <dw:transform-message doc:name="Transform Message">
    <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
    name:"Peter",
    age:"42"
}]]></dw:set-payload>
  </dw:transform-message>
  <mongo:insert-document config-ref="Mongo_DB__Configuration" collection="People" doc:name="Insert Document"/>
</flow>

See Also