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.

Create a Facebook Messenger Bot using MuleSoft and the Anypoint Platform.

With over 900 million users a month, the Facebook Messenger platform has a pretty big audience.  The introduction of Messenger Bots earlier this year helped extend this adoption by helping drive personalized, scaled experiences with customers. Instead of building an app, just build the message thread as the place where you connect and interact with your users.

I was curious though to see how easy it was to build my own Bot using MuleSoft and the Anypoint Platform. Instead of writing custom code and running my own dedicated server, I leveraged Anypoint Studio to configure an app and CloudHub for my server needs. The following is a walk-through that shows you how you can create your own Facebook Messenger Bot.

Setup Facebook App and Page

To get started, setup a new app in Facebook under the developers site: https://developers.facebook.com/apps/

Add New App for Messenger

Be sure to select Apps for Messenger under the Category dropdown before clicking Create App ID. 

Once the app gets created, scroll down to the Token Generation section and click on Create a new pageYou’ll be re-directed to the Facebook site to create a new page.

Create a new page

Select any category (I selected Brand or Product > Software) and give it a name (e.g. MuleSoft Demos). Skip through the setup steps and jump back to the Facebook Developer site. Once you refresh the page, the new Facebook Page with show under the Token Generation section under the Page dropdown.

Select your new page and it will create a Page Access Token. You’ll need this for your Mule app to call the Send API later.

Page Access Token

Deploy App to CloudHub

Next, deploy the example app to CloudHub. You can download a copy of the project from Github here: https://github.com/djuang1/mule-facebook-bot and import it into Anypoint Studio.

Before you deploy it to CloudHub, open the the mule-app.properties file and copy and paste the Page Access Token to the access_token variable.


The example app is built to demonstrate a few of the the required event listeners for the Facebook Messenger platform. It handles the webhooks for messages and postbacks. Messages are parsed for keywords such as ‘template’, ‘button’, etc.. and all other messages are echoed back to the user using the Send API.

When the app is deployed and running in CloudHub, navigate to the app URL (e.g http://facebook-bot.cloudhub.io/) and test out the webhook challenge using the following URL:


You should get the challenge returned like the following screenshot:

Setup Webhooks

With the challenge returned successfully, we can setup the webhooks in Facebook now. Switch back to the Facebook Developers site and click on Setup Webhooks under the Webhooks section.

Enter the app URL under Callback URL with the path webhook. Enter any text under the Verify Token field. Under the Subscription Fields section, check messages and messaging_postbacks and then click on Verify and Save.

Once the webhook setup is complete, select the Facebook Page that was created previously and click on Subscribe in the Webhooks section.

Test Messages

Everything should be setup and running now on both CloudHub and Facebook. Let’s go ahead and test out the messages. Switch back to Facebook and open up the Page you created. You should see a button that says Send Message. Mouseover that button and then click on Test Button.

Go ahead and test out various messages and see their responses. When you type in messages that contain keywords such as button or template, you’ll see specific message formats returned.

You can also review the live console in CloudHub for the app to see the logs for the request and response.


If you go back and look at the flow in Anypoint Studio, it’s a surprisingly simple flow that handles the requests and response from the Facebook Messenger platform. The benefit of using MuleSoft is the ability to route messages to other platforms without having to write code. A keyword for ‘orders’ can be routed to Salesforce to retrieve a list of orders for the user using the Salesforce Connector. Try out the code and let me know if you run into any issues or create a useful Bot for Facebook.