Class BatchQuery
java.lang.Object
com.github.darksoulq.abyssallib.common.database.nosql.mongodb.BatchQuery
Handles high-performance bulk write operations in MongoDB.
This class allows queuing multiple different operation types (insert, replace, update, delete)
and executing them in a single network round-trip to optimize database performance.
-
Constructor Summary
ConstructorsConstructorDescriptionBatchQuery(Database database, String collectionName) Constructs a new BatchQuery for a specific collection. -
Method Summary
Modifier and TypeMethodDescriptiondelete(org.bson.Document filter) Queues a delete operation to remove all documents matching the filter.longexecute()Executes all queued bulk write operations synchronously.Executes all queued bulk write operations asynchronously using the database thread pool.insert(org.bson.Document document) Queues an insert operation for a single document.replace(org.bson.Document filter, org.bson.Document replacement, boolean upsert) Queues a replace operation that swaps a document matching the filter with a new one.update(org.bson.Document filter, org.bson.Document update, boolean upsert) Queues an update operation to modify multiple documents matching the filter.
-
Constructor Details
-
BatchQuery
-
-
Method Details
-
insert
Queues an insert operation for a single document.- Parameters:
document- The BSONDocumentto be inserted into the collection.- Returns:
- This
BatchQueryinstance for fluent method chaining.
-
replace
Queues a replace operation that swaps a document matching the filter with a new one.- Parameters:
filter- The BSONDocumentdefining the criteria to find the document to replace.replacement- The new BSONDocumentthat will take the place of the matched document.upsert- If true, a new document is created if no document matches the filter.- Returns:
- This
BatchQueryinstance for fluent method chaining.
-
update
Queues an update operation to modify multiple documents matching the filter.- Parameters:
filter- The BSONDocumentdefining which documents should be updated.update- The BSONDocumentcontaining the fields and values to set.upsert- If true, a new document is created if no documents match the filter.- Returns:
- This
BatchQueryinstance for fluent method chaining.
-
delete
Queues a delete operation to remove all documents matching the filter.- Parameters:
filter- The BSONDocumentdefining the criteria for documents to be removed.- Returns:
- This
BatchQueryinstance for fluent method chaining.
-
execute
public long execute()Executes all queued bulk write operations synchronously.- Returns:
- The combined total count of documents that were inserted, modified, or deleted.
-
executeAsync
Executes all queued bulk write operations asynchronously using the database thread pool.- Returns:
- A
CompletableFuturethat will yield the total affected document count upon completion.
-