Class BatchQuery

java.lang.Object
com.github.darksoulq.abyssallib.common.database.nosql.mongodb.BatchQuery

public class BatchQuery extends Object
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

    Constructors
    Constructor
    Description
    BatchQuery(Database database, String collectionName)
    Constructs a new BatchQuery for a specific collection.
  • Method Summary

    Modifier and Type
    Method
    Description
    delete(org.bson.Document filter)
    Queues a delete operation to remove all documents matching the filter.
    long
    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.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BatchQuery

      public BatchQuery(Database database, String collectionName)
      Constructs a new BatchQuery for a specific collection.
      Parameters:
      database - The target Database instance.
      collectionName - The name of the collection where operations will be applied.
  • Method Details

    • insert

      public BatchQuery insert(org.bson.Document document)
      Queues an insert operation for a single document.
      Parameters:
      document - The BSON Document to be inserted into the collection.
      Returns:
      This BatchQuery instance for fluent method chaining.
    • replace

      public BatchQuery 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.
      Parameters:
      filter - The BSON Document defining the criteria to find the document to replace.
      replacement - The new BSON Document that will take the place of the matched document.
      upsert - If true, a new document is created if no document matches the filter.
      Returns:
      This BatchQuery instance for fluent method chaining.
    • update

      public BatchQuery update(org.bson.Document filter, org.bson.Document update, boolean upsert)
      Queues an update operation to modify multiple documents matching the filter.
      Parameters:
      filter - The BSON Document defining which documents should be updated.
      update - The BSON Document containing the fields and values to set.
      upsert - If true, a new document is created if no documents match the filter.
      Returns:
      This BatchQuery instance for fluent method chaining.
    • delete

      public BatchQuery delete(org.bson.Document filter)
      Queues a delete operation to remove all documents matching the filter.
      Parameters:
      filter - The BSON Document defining the criteria for documents to be removed.
      Returns:
      This BatchQuery instance 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

      public CompletableFuture<Long> executeAsync()
      Executes all queued bulk write operations asynchronously using the database thread pool.
      Returns:
      A CompletableFuture that will yield the total affected document count upon completion.