Update Document with a MongoDB Function using MuleSoft

I recently had to figure out how to use the MuleSoft MongoDB Connector and update a document with a function. This post is a quick walk-through showing how to build an example flow to address the use case.

In MongoDB, the collection we’ll be performing the function against looks like the following. It’s just a set of employee records. We’ll run a query against the emp_no field and update the sequence_value field using the function $inc.

{
    "_id" : ObjectId("58421c6a1bdd54048cd86b19"),
    "emp_no" : 10001,
    "last_name" : "Facello",
    "first_name" : "Georgi",
    "gender" : "M",
    "hire_date" : "1986-06-26T00:00:00",
    "birth_date" : "1953-09-02T00:00:00",
    "sequence_value" : 7.0
}

Let’s get started, my environment for this post consisted of the following components all running locally on my Macbook.

  • Anypoint Studio
  • MongoDB – For testing purposes, I use mLab which provides a hosted Database-as-a-Service for MongoDB. You can found out more about the service here: https://mlab.com/

The first thing of course is to install the MongoDB Connector if you don’t already have it installed in Anypoint Studio. You can find it in Exchange. Here’s a link to the documentation on how to use the connector as well.

Once you have it installed, build the flow below using the following connectors like the screenshot below:

  • HTTP
  • Transform Message
  • MongoDB
  • Object to JSON

Next configure the HTTP connector with the default configuration (e.g. http://localhost:8081/)

Then click on the MongoDB connector and click on the Add button for the Connector Configuration and fill in the Username, Password, Database, and Servers (host:port) fields. You can test the connection and then click on OK.

Back on the Connector properties tab, setup the following:

  1. Select Update documents by function for the Operation field.
  2. Enter in a Collection name
  3. Enter in a Function. In our case we’ll be using $inc. I’m using one of the Field Update Operators that MongoDB provides to increment a field when a query matches a document. We’ll set the query in the Transform Message component after configuring the MongoDB connector.
  4. Set the Element Reference field to #[‘{sequence_value:1}’]. This will increment the sequence_value field by 1 when the query matches a document.
  5. Lastly, set the Query Reference field to #[flowVars.query]. This will contain the query from the DataWeave component.

The configuration for the MongoDB connector should look like the following:

Next, select the Transform Component and change the Output to a variable and set the Variable name to query.

Click OK and then paste the following script

%dw 1.0
%output application/java
---
{
    "emp_no": 10001
}

I’ve hardcoded the emp_no to match my record (e.g. 10001) in my collection but you can set it to be dynamic and pass in a value when you make an HTTP request.

The final Mule XML for the flow should look like the following:

<flow name="mongodb-example">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-variable variableName="query"><![CDATA[%dw 1.0
%output application/java
---
{
    "emp_no": 10001
}
]]></dw:set-variable>
        </dw:transform-message>
        <mongo:update-documents-by-function config-ref="Mongo_DB__Configuration" collection="employee" function="$inc" element-ref="#['{sequence_value:1}']" query-ref="#[flowVars.query]" doc:name="Mongo DB"/>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>

Go ahead and run your project now. When you make a request from your browser to http://localhost:8081, you’ll see the following response returned from the MongoDB connector.

The matchedCount will equal 1 showing that it matched a document in your collection. The modifiedCount signifies that a document was updated with the function.

Hopefully this post works for you. If you ran into any issues while setting this up or have any question, feel free to post to the comment section.


Posted

in

by

Comments