JDBC Driver for MongoDB

Build 24.0.9060

Batch Processing

The CData JDBC Driver for MongoDB enables you to take advantage of the bulk load support in MongoDB through the JDBC batch API. You can use the batch API to execute related SQL data manipulation statements simultaneously.

Using the JDBC Batch API

The following examples show how to execute bulk operations with the PreparedStatement class.

Bulk Insert

To perform a bulk insert with a PreparedStatement, call addBatch for each set of parameters you want to execute as part of the bulk insert. After adding all the sets of parameters to the batch, you can execute the bulk insert by calling executeBatch.

The executeBatch method returns an array that contains the update counts for each statement. For example:

String query = "INSERT INTO [CData].[Sample].Customers (CompanyName) VALUES (?)"; 
PreparedStatement pstmt = conn.prepareStatement(query);

pstmt.setString(1, "Jon Deere"); 
pstmt.addBatch();

pstmt.setString(1, "Caterpillar"); 
pstmt.addBatch();

int[] r = pstmt.executeBatch();
for(int i: r)
  System.out.println(i);

Bulk Update

To perform a bulk update with a PreparedStatement, call addBatch for each set of parameters you want to execute as part of the bulk update. After adding all the sets of parameters to the batch, call executeBatch.

The executeBatch method returns the update counts for each statement in an array. For example:

String query = "UPDATE [CData].[Sample].Customers SET CompanyName = ? WHERE _id = ?"; 
PreparedStatement pstmt = conn.prepareStatement(query);

pstmt.setString(1,"Jon Deere");
pstmt.setString(2,"MyId"); 
pstmt.addBatch();		

pstmt.setString(1,"Caterpillar");
pstmt.setString(2,"MyId2"); 
pstmt.addBatch();		

int[] r = pstmt.executeBatch();
for(int i: r)
  System.out.println(i);

Bulk Delete

To perform a bulk delete with a PreparedStatement, call addBatch for each set of parameters you want to execute as part of the bulk delete. After adding all the sets of parameters to the batch, call executeBatch.

The executeBatch method returns the update counts for each statement in an array. For example:

String query = "DELETE FROM [CData].[Sample].Customers WHERE _id = ?";
PreparedStatement pstmt = conn.prepareStatement(query);

pstmt.setString(1,"MyId1"); 
pstmt.addBatch();		

pstmt.setString(1,"MyId2");
pstmt.addBatch();		

int[] r = pstmt.executeBatch();
for(int i: r)
  System.out.println(i);

Setting the Batch Size

Setting the maximum batch size can be necessary when the server has limitations on the size of the request that can be submitted. Set the BatchSize property to split the entire batch into batches of the specified value. Each batch is submitted to the server individually.

Or, set BatchSize to 0 to submit the entire batch.

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 24.0.9060