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.

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

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:

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.

Author: Dejim Juang

I am a technology junkie, proud father, loving husband, social media user, amateur photographer, and runner. Originally from Chicago, I currently reside in Houston, TX. I work as a Principal Solutions Architect for MuleSoft, and my work allows me to dabble with the latest web technologies.