Cloud Connector of the Week: MongoDB

motif

This section of our blog is about showcasing current and upcoming cloud connectors. Cloud Connectors are Mule modules that allow easy connectivity to cloud APIs such as Twiiter, Facebook, and the thousands of Software as a Service platform out there on the net.

This week will talk about Mongo. Mongo is very popular database nowadays, and for several reasons. It is being use in production by several well known companies such a Disney, The New York Times, Foursquare, Chicago Tribune among several others.

MongoDB

MongoDB is an open source, document-oriented database written in the C++ programming language. The database is document-oriented so it manages collections of JSON-like documents.

MongoDB maintains many of the great features of a relational database — like indexes and dynamic queries. But by changing the data model from relational to document-oriented, you gain many advantages, including greater agility through flexible schemas and easier horizontal scalability.

The goal of MongoDB is to bridge the gap between key-value stores (which are fast and scalable) and relational databases (which have rich functionality).

Adding MongoDB to your Mule App

So, what if you want to use the cloud connector in your Mule app? Really easy, first add the following snippet to your Maven POM:

then add the connector as a dependency:

That’s it!

Using MongoDB Cloud Connector in your flow

The first thing you should do is declare the MongoDB namespace in your flow as follows:

then, we need to declare the connector and configure it (remember that you can have as many configurations as you want):

Notice that in the previous example we are using property placeholders loading the actual values of the configuration from properties file.

Every configuration attribute is optional. If you are running Mongo locally using default port and in un-authenticated mode then you can just configure the connection using <mongo:config/>

Inserting an Object into a Collection

This operations receives a collection name and an object. The object can either be a raw DBObject, a String-Object Map or a JSon String. If it is passed as Map, a shallow conversion into DBObject is performed – that is, no conversion is performed to its values. If it is passed as JSon String, _ids of type ObjectId must be passed as a String.

The following is an example invocation using a Map:

Creating a new Collection

This operation will create a new collection or throw a MongoException if the collection already exists.

You can optionally specify a cap if you want to.

Inserting a File

MongoDB can store regular files inside its GridFS architecture.

 

This snippet will upload a file named myFilename.xml using the payload of the message as its content.

Map/Reduce

Map/Reduce in MongoDB is useful for batch processing of data and aggregation operations. It is similar in spirit to using something like Hadoop with all input coming from a collection and output going to a collection. Often, in a situation where you would have used GROUP BY in SQL, map/reduce is the right tool in MongoDB.

The following snippet will emit different temperatures for each weather station and reduce the result by calculating an average between them:

More Info

This is just a short preview of the kind of things you can do with our MongoDB connector. I only showed you 4 operations but the connector itself has well over 20. It can manage collections, indexes, files inside GridFS and of course documents. If you like to know more about MongoDB you can check their home page there:

MongoDB

Also, for more info about our connector you can cheeck our page at MuleForge:

MongoDB Cloud Connector

And for the curious out there we offer full source code for the connector along with two full examples at our GitHub repository:

MongoDB Cloud Connector GitHub Repository

MongoDB Example – Inserting Products

MongoDB Example – Map Reduce


We'd love to hear your opinion on this post


One Response to “Cloud Connector of the Week: MongoDB”

  1. Fantastic. Thanks Emiliano.