Class CollectionQuery

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

public class CollectionQuery extends Object
A fluent builder for executing CRUD operations on a single MongoDB collection.
  • Constructor Details

    • CollectionQuery

      public CollectionQuery(Database database, String collectionName)
      Constructs a new CollectionQuery.
      Parameters:
      database - The target Database.
      collectionName - The name of the collection.
  • Method Details

    • insert

      public CollectionQuery insert()
      Sets operation type to INSERT. @return this.
    • replace

      public CollectionQuery replace()
      Sets operation type to REPLACE. @return this.
    • update

      public CollectionQuery update()
      Sets operation type to UPDATE. @return this.
    • delete

      public CollectionQuery delete()
      Sets operation type to DELETE. @return this.
    • value

      public CollectionQuery value(String key, Object value)
      Adds a key-value pair to the write payload. @return this.
    • values

      public CollectionQuery values(org.bson.Document doc)
      Adds all fields from a document to the write payload. @return this.
    • filter

      public CollectionQuery filter(String key, Object value)
      Adds a filter criterion. @return this.
    • filter

      public CollectionQuery filter(org.bson.Document doc)
      Adds multiple filter criteria. @return this.
    • sort

      public CollectionQuery sort(String key, boolean ascending)
      Sets the sort order.
      Parameters:
      key - The field to sort by.
      ascending - True for 1, false for -1.
      Returns:
      this.
    • limit

      public CollectionQuery limit(int limit)
      Sets the result limit. @return this.
    • skip

      public CollectionQuery skip(int skip)
      Sets the result skip count. @return this.
    • upsert

      public CollectionQuery upsert(boolean upsert)
      Sets the upsert flag. @return this.
    • batch

      public BatchQuery batch()
      Transitions into a batch operation for bulk writes.
      Returns:
      A new BatchQuery instance.
    • execute

      public long execute()
      Executes the configured operation synchronously.
      Returns:
      The count of affected documents.
    • executeAsync

      public CompletableFuture<Long> executeAsync()
      Executes the configured operation asynchronously.
      Returns:
      A future containing the affected document count.
    • count

      public long count()
      Counts documents matching the filter.
      Returns:
      Total count.
    • exists

      public boolean exists()
      Checks if any documents match the filter.
      Returns:
      True if count > 0.
    • first

      public <R> R first(DocumentMapper<R> mapper)
      Retrieves the first matching document and maps it.
      Type Parameters:
      R - Result type.
      Parameters:
      mapper - The mapper logic.
      Returns:
      The mapped object or null.
    • select

      public <R> List<R> select(DocumentMapper<R> mapper)
      Retrieves all matching documents and maps them to a list.
      Type Parameters:
      R - Result type.
      Parameters:
      mapper - The mapper logic.
      Returns:
      A list of mapped results.
    • selectAsync

      public <R> CompletableFuture<List<R>> selectAsync(DocumentMapper<R> mapper)
      Retrieves all matching documents asynchronously.
      Type Parameters:
      R - Result type.
      Parameters:
      mapper - The mapper logic.
      Returns:
      A future containing the result list.