Interface BulkOperations


public interface BulkOperations
Bulk operations for insert/update/remove actions on a collection. Bulk operations are available since MongoDB 2.6 and make use of low level bulk commands on the protocol level. This interface defines a fluent API to add multiple single operations or list of similar operations in sequence which can then eventually be executed by calling execute().

Bulk operations are issued as one batch that pulls together all insert, update, and delete operations. Operations that require individual operation results such as optimistic locking (using @Version) are not supported and the version field remains not populated.

Since:
1.9
Author:
Tobias Trelle, Oliver Gierke, Minsu Kim
  • Method Details

    • insert

      BulkOperations insert(Object documents)
      Add a single insert to the bulk operation.
      Parameters:
      documents - the document to insert, must not be null.
      Returns:
      the current BulkOperations instance with the insert added, will never be null.
    • insert

      BulkOperations insert(List<? extends Object> documents)
      Add a list of inserts to the bulk operation.
      Parameters:
      documents - List of documents to insert, must not be null.
      Returns:
      the current BulkOperations instance with the insert added, will never be null.
    • updateOne

      BulkOperations updateOne(Query query, Update update)
      Add a single update to the bulk operation. For the update request, only the first matching document is updated.
      Parameters:
      query - update criteria, must not be null.
      update - Update operation to perform, must not be null.
      Returns:
      the current BulkOperations instance with the update added, will never be null.
    • updateOne

      BulkOperations updateOne(List<Pair<Query,Update>> updates)
      Add a list of updates to the bulk operation. For each update request, only the first matching document is updated.
      Parameters:
      updates - Update operations to perform.
      Returns:
      the current BulkOperations instance with the update added, will never be null.
    • updateMulti

      BulkOperations updateMulti(Query query, Update update)
      Add a single update to the bulk operation. For the update request, all matching documents are updated.
      Parameters:
      query - Update criteria.
      update - Update operation to perform.
      Returns:
      the current BulkOperations instance with the update added, will never be null.
    • updateMulti

      BulkOperations updateMulti(List<Pair<Query,Update>> updates)
      Add a list of updates to the bulk operation. For each update request, all matching documents are updated.
      Parameters:
      updates - Update operations to perform.
      Returns:
      the current BulkOperations instance with the update added, will never be null.
    • upsert

      BulkOperations upsert(Query query, Update update)
      Add a single upsert to the bulk operation. An upsert is an update if the set of matching documents is not empty, else an insert.
      Parameters:
      query - Update criteria.
      update - Update operation to perform.
      Returns:
      the current BulkOperations instance with the update added, will never be null.
    • upsert

      BulkOperations upsert(List<Pair<Query,Update>> updates)
      Add a list of upserts to the bulk operation. An upsert is an update if the set of matching documents is not empty, else an insert.
      Parameters:
      updates - Updates/insert operations to perform.
      Returns:
      the current BulkOperations instance with the update added, will never be null.
    • remove

      BulkOperations remove(Query remove)
      Add a single remove operation to the bulk operation.
      Parameters:
      remove - the Query to select the documents to be removed, must not be null.
      Returns:
      the current BulkOperations instance with the removal added, will never be null.
    • remove

      BulkOperations remove(List<Query> removes)
      Add a list of remove operations to the bulk operation.
      Parameters:
      removes - the remove operations to perform, must not be null.
      Returns:
      the current BulkOperations instance with the removal added, will never be null.
    • replaceOne

      default BulkOperations replaceOne(Query query, Object replacement)
      Add a single replace operation to the bulk operation.
      Parameters:
      query - Update criteria.
      replacement - the replacement document. Must not be null.
      Returns:
      the current BulkOperations instance with the replace added, will never be null.
      Since:
      2.2
    • replaceOne

      BulkOperations replaceOne(Query query, Object replacement, FindAndReplaceOptions options)
      Add a single replace operation to the bulk operation.
      Parameters:
      query - Update criteria.
      replacement - the replacement document. Must not be null.
      options - the FindAndModifyOptions holding additional information. Must not be null.
      Returns:
      the current BulkOperations instance with the replace added, will never be null.
      Since:
      2.2
    • execute

      com.mongodb.bulk.BulkWriteResult execute()
      Execute all bulk operations using the default write concern.
      Returns:
      Result of the bulk operation providing counters for inserts/updates etc.
      Throws:
      BulkOperationException - if an error occurred during bulk processing.