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()
.
MongoOperations ops = …; ops.bulkOps(BulkMode.UNORDERED, Person.class) .insert(newPerson) .updateOne(where("firstname").is("Joe"), Update.update("lastname", "Doe")) .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
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic enum
Mode for bulk operation. -
Method Summary
Modifier and TypeMethodDescriptioncom.mongodb.bulk.BulkWriteResult
execute()
Execute all bulk operations using the default write concern.Add a single insert to the bulk operation.Add a list of inserts to the bulk operation.Add a list of remove operations to the bulk operation.Add a single remove operation to the bulk operation.default BulkOperations
replaceOne
(Query query, Object replacement) Add a single replace operation to the bulk operation.replaceOne
(Query query, Object replacement, FindAndReplaceOptions options) Add a single replace operation to the bulk operation.updateMulti
(List<Pair<Query, UpdateDefinition>> updates) Add a list of updates to the bulk operation.default BulkOperations
updateMulti
(Query query, Update update) Add a single update to the bulk operation.updateMulti
(Query query, UpdateDefinition update) Add a single update to the bulk operation.updateOne
(List<Pair<Query, UpdateDefinition>> updates) Add a list of updates to the bulk operation.default BulkOperations
Add a single update to the bulk operation.updateOne
(Query query, UpdateDefinition update) Add a single update to the bulk operation.Add a list of upserts to the bulk operation.default BulkOperations
Add a single upsert to the bulk operation.upsert
(Query query, UpdateDefinition update) Add a single upsert to the bulk operation.
-
Method Details
-
insert
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
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
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
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. - Since:
- 4.1
-
updateOne
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
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
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. - Since:
- 4.1
-
updateMulti
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
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
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. - Since:
- 4.1
-
upsert
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
Add a single remove operation to the bulk operation.- Parameters:
remove
- theQuery
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
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
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 replacement added, will never be null. - Since:
- 2.2
-
replaceOne
Add a single replace operation to the bulk operation.- Parameters:
query
- Update criteria.replacement
- the replacement document. Must not be null.options
- theFindAndModifyOptions
holding additional information. Must not be null.- Returns:
- the current
BulkOperations
instance with the replacement 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.
-