MongoDB Connector Example

The MongoDB Connector can be used to perform CRUD operations in the local database as well as in MongoDB Atlas (cloud version of MongoDB).

What you'll build

This example explains how to use MongoDB Connector to insert and find documents from a MongoDB database.

The sample API given below demonstrates how the MongoDB connector can be used to connect to the MongoDB Server and perform insert many and find operations on it.

  • /insertmany: The user sends a request payload that includes the connection information, collection name, and the documents to be inserted. This request is sent to the integration runtime by invoking the MongodbConnector API. This will insert the documents into the MongoDB database.

    Insert many function

  • /find: The user sends the request payload containing the connection information, collection name, and the find query. This request is sent to the integration runtime by invoking the MongodbConnector API. Once the API is invoked, it returns the documents matching the find query.

    Find function

If you do not want to configure this yourself, you can simply get the project and run it.

Before you begin

If you want to connect to MongoDB Atlas, follow the steps mentioned below to get the connection string.

  1. In the Clusters view, click Connect for the cluster to which you want to connect.

  2. Click Choose a connection method.

  3. Click Connect your application.

  4. Select Java from the Driver menu.

  5. Select the correct driver version from the Version menu.

  6. Clear the Include full driver code example check box to get the connection string.

Configure the connector in ESB Integration Studio

Follow these steps to set up the Integration Project and the Connector Exporter Project.

  1. Open ESB Integration Studio and create an Integration Project. Creating a new Integration Project

  2. Right-click the project that you created and click on Add or Remove Connector -> Add Connector. You will get directed to the Connector Store.

  3. Search for the specific connector required for your integration scenario and download it to the workspace. Search Connector in the Connector Store

  4. Click Finish, and your Integration Project is ready. The downloaded connector is displayed on the side palette with its operations.

  5. You can drag and drop the operations to the design canvas and build your integration logic. Drag connector operations

  6. Right click on the created Integration Project and select New -> Rest API to create the REST API.

Creating the Integration Logic

  1. Create a new integration project named MongodbConnector. Be sure to enable a Connector Exporter.

    Create project

  2. Right-click the created Integration Project and select, -> New -> Rest API to create the REST API.

    Adding a Rest API

  3. Provide the API name as MongoConnector and the API context as /mongodbconnector.

  4. First, create the /insertmany resource. This API resource inserts documents into the MongoDB database.
    Right-click on the API Resource and go to the Properties view. Let's use a URL template called /insertmany as there are two API resources inside a single API. The method is Post.

    Adding the API resource.

  5. Drag the 'insertMany' operation of the MongoDB Connector to the Design view as shown below.

    Adding the insert many operation.

  6. Create a connection from the Properties view by clicking the '+' icon as shown below.

    Following values can be provided when connecting to the MongoDB database.

    • Connection Name - connectionURI
    • Connection Type - URI
    • Connection URI - mongodb+srv://server.example.com/?connectTimeoutMS=300000&authSource=aDifferentAuthDB
    • Database - TestDatabase

    Adding the connection.

  7. After the connection is successfully created, you can select the new connection from the 'Connection' menu in the properties view.

    Selecting the connection.

  8. Next, provide JSON expressions for the following two properties. These expressions will retrieve the respective values from the JSON request payload.

    • Collection - json-eval($.collection)
    • Documents - json-eval($.documents)
  9. Drag the Respond Mediator to the canvas. This returns the response message to the client (after inserting documents) as shown below.

    Adding the respond mediator.

  10. Create the next API resource (which is /find) by dragging another API resource to the Design view. This API resource will find all the documents matching the find query given by the user. This will also be a POST request.

  11. Drag the find operation of the Email Connector to the Design view as shown below.

  12. Select 'connectionURI' as the connection from the 'Connection' menu in the properties view.

  13. Next, provide JSON expressions for the following two properties. These expressions will retrieve the respective values from the JSON request payload.

    • Collection - json-eval($.collection)
    • Query - json-eval($.query)
  14. Drag the Respond Mediator to the canvas. This returns the response message to the client (after retrieving documents) as shown below.

  15. You can find the complete API XML configuration below. You can go to the source view and copy paste the following config.

<?xml version="1.0" encoding="UTF-8"?>
<api context="/mongodbconnector" name="MongodbConnector" xmlns="http://ws.apache.org/ns/synapse">
    <resource methods="POST" uri-template="/insertmany">
        <inSequence>
            <mongodb.insertMany configKey="connectionURI">
                <collection>{json-eval($.collection)}</collection>
                <documents>{json-eval($.documents)}</documents>
                <ordered>True</ordered>
            </mongodb.insertMany>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
    <resource methods="POST" uri-template="/find">
        <inSequence>
            <mongodb.find configKey="connectionURI">
                <collection>{json-eval($.collection)}</collection>
                <query>{json-eval($.query)}</query>
            </mongodb.find>
            <respond/>
        </inSequence>
        <outSequence/>
        <faultSequence/>
    </resource>
</api>

Exporting Integration Logic as a CApp

CApp (Carbon Application) is the deployable artifact on the integration runtime. Let us see how we can export integration logic we developed into a CApp along with the connector.

Creating Connector Exporter Project

To bundle a Connector into a CApp, a Connector Exporter Project is required.

  1. Navigate to File -> New -> Other -> WSO2 -> Extensions -> Project Types -> Connector Exporter Project.

    Add Connector Exporter Project

  2. Enter a name for the Connector Exporter Project.

  3. In the next screen select, Specify the parent from workspace and select the specific Integration Project you created from the dropdown.
    Naming Connector Exporter Project

  4. Now you need to add the Connector to Connector Exporter Project that you just created. Right-click the Connector Exporter Project and select, New -> Add Remove Connectors -> Add Connector -> Add from Workspace -> Connector

  5. Once you are directed to the workspace, it displays all the connectors that exist in the workspace. You can select the relevant connector and click Ok.

    Selecting Connector from Workspace

Creating a Composite Application Project

To export the Integration Project as a CApp, a Composite Application Project needs to be created. Usually, when an Integration project is created, this project can be created as part of that project by Integration Studio. If not, you can specifically create it by navigating to File -> New -> Other -> WSO2 -> Distribution -> Composite Application Project.

Exporting the Composite Application Project

  1. Right-click the Composite Application Project and click Export Composite Application Project.

    Export as a Carbon Application

  2. Select an Export Destination where you want to save the .car file.

  3. In the next Create a deployable CAR file screen, select both the created Integration Project and the Connector Exporter Project to save and click Finish. The CApp is created at the specified location provided at the previous step.

    Create a deployable CAR file

Get the project

You can download the ZIP file and extract the contents to get the project code.

Download ZIP

Deployment

Follow these steps to deploy the exported CApp to the integration runtime.

Deploying on Micro Integrator

You can copy the composite application to the <PRODUCT-HOME>/repository/deployment/server/carbonapps folder and start the server. Micro Integrator will be started and the composite application will be deployed.

You can further refer the application deployed through the CLI tool. See the instructions on managing integrations from the CLI.

Click here for instructions on deploying on ESB Enterprise Integrator 6
  1. You can copy the composite application to the <PRODUCT-HOME>/repository/deployment/server/carbonapps folder and start the server.

  2. ESB EI server starts and you can login to the Management Console https://localhost:9443/carbon/ URL. Provide login credentials. The default credentials will be admin/admin.

  3. You can see that the API is deployed under the API section.

Click here for instructions on removing the iterative mongodb server logs

Add the configuration below to remove the iterative org.mongodb.driver.cluster server logs;

1.  Add the following logger to the `log4j2.properties` file in the `<PRODUCT_HOME>/conf` folder.

    ```xml
    logger.org-mongodb-driver-cluster.name = org.mongodb.driver.cluster
    logger.org-mongodb-driver-cluster.level = WARN
    ```

2.  Then, add `org-mongodb-driver-cluster` to the list of `loggers`.

!!! Prerequisite 1. Download the Mongo java driver from here.

2. Add the driver to the `<PRODUCT_HOME>/dropins` folder.

3. Restart the server.

Testing

Insert Many Operation

  1. Create a file named insertmany.json with the following payload:

    {
        "collection": "TestCollection",
        "documents": [
            {
                "name": "Jane Doe",
                "_id": "123"
            },
            {
                "name": "John Doe",
                "_id": "1234"
            },
            {
                "name": "Jane Doe",
                "_id": "12345"
            }
        ]
    }
  2. Invoke the API as shown below using the curl command.

    Info

    The Curl application can be downloaded from here.

    curl -H "Content-Type: application/xml" --request POST --data @insertmany.json http://localhost:8290/mongodbconnector/insertmany

    Expected Response : You should get a response as given below and the data will be added to the database.

    {
        "InsertManyResult": "Successful"
    }

Find Operation

Note

In order to find documents by ObjectId, the find query payload should be in the following format:

```json
{
    "query": {
        "_id": {
            "$oid": "6011b180007ce60ab2ad74a5"
        }
    }
}
```
  1. Create a file called find.json with the following payload.

    {
        "collection": "TestCollection",
        "query": {
            "name": "Jane Doe"
        }
    }
  2. Invoke the API using the curl command shown below.

    Info

    Curl Application can be downloaded from here.

    curl -H "Content-Type: application/xml" --request POST --data @find.json http://localhost:8290/mongodbconnector/find

    Expected Response : You should get a response similar to the one given below.

    [
        {
            "_id": "123",
            "name": "Jane Doe"
        },
        {
            "_id": "12345",
            "name": "Jane Doe"
        }
    ]

What's Next

Top