Using Bulk API with MongoDB Connector

November 1 2016

0 comments 0
mongodb-connector-banners

You can visit the MongoDB Connector page here.

With MongoDB as one of the most popular NoSQL databases, we are excited to announce the release of our v4.2.0. This version includes improvements in connector configuration and support for batch/bulk operations. Let’s walk through an example of using the bulk operation (Bulk.insert()) using the MongoDB Connector v4.2.0.

Since v2.6, MongoDB has supported bulk operations. If you list bulk operations and execute them with Bulk.execute(), MongoDB groups your operations (up to 1000 operations) by operation type. Among these bulk operations, Anypoint Connector for MongoDB v4.2.0 supports Bulk.insert().

Before getting started, please make sure you have v4.2.0 downloaded in Anypoint Studio. If not, please download it from Exchange.

screen-shot-2016-09-21-at-11-31-42-am

 

Find the demo-batch app from this page and import it into Studio. The following app shows you two different use cases: using “Insert Documents” (bulk operation) flow as is, or using it in a batch flow.

screen-shot-2016-09-21-at-11-35-52-am

 

Once you import the demo app, select “Global Element” and open the MongoDB configuration by clicking on “Edit.”

screen-shot-2016-09-21-at-11-39-06-am

You can specify your MongoDB configuration here, but I recommend you use the mule-app.properties. In mule-app.properties, configure the following keys:

username=
password=
database=
host=
collection=demo (you can choose a different collection.)

If you start the demo app in Studio and hit localhost:8081/ with your browser, the following documents will be added to the collection called “demo”. In this case, the first set of documents will be added by “Bulk Insert” and the next set of document will be added by “Bulk Insert” in batch.

Before the “Insert Documents (Bulk.insert())” operation, if a user wanted to insert 5,000 documents into MongoDB, they needed to use the “Insert Document” operation 5,000 times. In this case – if – for example, the 500th document failed to be inserted to MongoDB, the rest of 4,500 documents would not be inserted. Now, with the same scenario, you can not only use “Insert Documents” once to insert all documents, and the one failed document would not hinder the rest of the documents from being inserted.

Also, you can use the “Insert Documents” operation in a flow or a batch flow. If you use it in the flow and process a huge amount of documents, you need to make sure you have enough memory or manage memory properly not to be overflowed. However, if you use it in a batch flow, the batch will deal with memory management for the huge documents, and you can focus on managing the failed documents.

For new users, try the above example to get started, and for others, please share with us how you use or are planning to use “Insert Documents” in the MongoDB Connector! Also, explore the Anypoint Exchange to see other resources you can leverage today.


 


We'd love to hear your opinion on this post