Skip to content

Mongo Batch Insert Update

Devender Yadav edited this page Nov 8, 2017 · 7 revisions

Batch insert/update is an important feature keeping use cases of NoSQL and system's performance in mind.

Feature Available

MongoDB, from version 2.6 has native support available for performing bulk operations. Kundera also enabled this support via its kundera-mongo module.

How to Use

Persistence.xml

Modify persistence.xml to add a property:

<property name="kundera.batch.size" value="5000" />

You can configure value as required batch size.

Order of Bulk Operation

MongoDB categorizes Bulk operations as :

ORDERED: In which requests included in the bulk operations will be executed in order and will halt on the first failure.

UNORDERED: The requests included in the bulk operation will be executed in an undefined order, and all requests will be executed even if some fail. Write requests included in the bulk operations will be executed in order

By default, bulk operations are un-ordered via Kundera-MongoDB . But you can modify this by changing the value of this simple property ORDERED/UNORDERED using boolean true/false for a particular entityManager instance.

 em = emf.createEntityManager();
 em.setProperty(MongoDBClientProperties.ORDERED_BULK_OPERATION, true);

Also, by default, operations in MongoDB will be performed with ACKNOWLEDGED WriteConcern. However, you can also change write concern for a particular entityManager instance using:

em.setProperty(MongoDBClientProperties.WRITE_CONCERN, WriteConcern.UNACKNOWLEDGED);

Sample Example

MongoBatchProcessorTest.

Under the Hood

Kundera uses methods of BulkWriteOperation from Mongo Java Driver to perform this bulk insert & update.

Clone this wiki locally