Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

MongoDB Connector

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.

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.

MongoDB Connector is an SDK-based connector developed with DevKit 3.7.1.

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

3.0 and above

Java

JDK 7 and above

Installing and Configuring

Installing

To install the MongoDB Connector:

  1. Open Anypoint Studio and got to Help > Install New Software

  2. Select Anypoint Connectors Update Site - http://repository.mulesoft.org/connectors/releases/3.5.0

  3. Expand Community and scroll downs to locate the MongoDB Connector.

    Anypoint Studio Install Window

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

  5. Restart Studio when prompted.

For more information, see Installing Connectors.

After installing the connector, your Studio palette contains the following.

Anypoint Studio palette - MongoDB Connector

Updating from an Older Version

  • Uninstall the previous MongoDB Connector.

  • Install the latest version of MongoDB Connector from the update site.

  • Ensure that the Maven dependencies have been updated correctly from Mongo DB[v3.X.X] to Mongo DB[v4.1.0].

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

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

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

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

    • 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 4.1.0 Migration Guide.

Using This Connector

Start by inserting:

  • Your MongoDB credentials and endpoints in a .properties file and load them using the Property Placeholder component.

  • An inbound endpoint into your flow. For example, HTTP.

  • A Transform Message component.

  • A MongoDB transformer for example, Document to json.

    MongoDB Connector initial flow

Setting up the Global Configuration

To setup the Global Configuration:

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

  2. Inside the filter textbox 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 should contain the corresponding password.

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

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

    5. 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 more information see Configuring Properties) for the Connection field values. 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

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.

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 check SSL property field.

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

Invoking an Operation

To invoke a simple operation such as the insertDocument 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:

    
                 
              
    1
    2
    3
    4
    5
    6
    7
    
    %dw 1.0
    %output application/java
    ---
    {
            name:"Peter",
            age:"42"
    }

    Transform Message

Transform Message


    
             
          
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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"/>
        <mongo:document-to-json doc:name="Document to Json"/>
</flow>

See Also