> ## Documentation Index
> Fetch the complete documentation index at: https://docs.ditto.live/llms.txt
> Use this file to discover all available pages before exploring further.

# Count docs with Legacy QL

> Count the number of documents in a collection that match a specified query. This endpoint is useful for pagination, analytics, and understanding the size of your dataset.

<Warning>
  This is a legacy endpoint that uses the [Legacy Query Language](/dql/query-syntax-legacy).

  To use [DQL](/dql), the current Ditto query language, consider using the [execute](./post-storeexecute) endpoint.
</Warning>


## OpenAPI

````yaml post /store/count
openapi: 3.0.3
info:
  title: Ditto HTTP RPC API
  version: 4.0.0
  description: >-
    The Ditto HTTP RPC API provides a RESTful interface for interacting with
    Ditto's distributed data store. It enables you to query, insert, update and
    delete data across your Ditto network while maintaining strong consistency
    guarantees.
servers:
  - url: '{base_url}/api/v4'
    description: >-
      The Ditto Big Peer acts as a central synchronization point and data store
      in your Ditto network. It coordinates data replication between peers and
      provides a consistent view of your data.
    variables:
      base_url:
        default: https://REPLACE_WITH_MY_APP_ID.cloud.ditto.live
        description: >-
          Your unique Ditto application endpoint URL, found in the Ditto Portal
          under Application Settings. Replace REPLACE_WITH_MY_APP_ID with your
          actual app ID.
security:
  - api_key_or_jwt_token: []
externalDocs:
  url: https://docs.ditto.live/http/installation/
  description: >-
    For more detailed instructions on how to use this API and the Ditto SDK,
    please see the documentation.
paths:
  /store/count:
    post:
      description: >-
        Count the number of documents in a collection that match a specified
        query. This endpoint is useful for pagination, analytics, and
        understanding the size of your dataset.
      operationId: count_endpoint
      parameters:
        - name: X-DITTO-TXN-ID
          in: header
          description: >-
            Optional transaction ID header that ensures consistency. If
            specified, the operation will only proceed if the Big Peer's
            transaction ID is equal to or greater than this value. This prevents
            reading stale data by ensuring you're operating on a sufficiently
            up-to-date version of the database.
          required: false
          deprecated: false
          schema:
            type: integer
            format: int64
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CountRequest'
        required: true
      responses:
        '200':
          description: >-
            The count operation completed successfully. Returns the total number
            of matching documents and the transaction ID of the operation.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/CountResponse'
            application/cbor:
              schema:
                $ref: '#/components/schemas/CountResponse'
        '400':
          description: >-
            The request was malformed. This typically indicates an invalid query
            syntax, unknown collection, or invalid parameters. Check the error
            message for specific details.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RpcError'
            application/cbor:
              schema:
                $ref: '#/components/schemas/RpcError'
        '401':
          description: >-
            Authentication failed. Ensure you're providing a valid API key or
            JWT token in the Authorization header.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RpcError'
            application/cbor:
              schema:
                $ref: '#/components/schemas/RpcError'
        '500':
          description: >-
            An unexpected error occurred on the server. This could indicate a
            temporary service disruption or internal error. Retry the request
            after a brief delay.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/RpcError'
            application/cbor:
              schema:
                $ref: '#/components/schemas/RpcError'
      deprecated: false
components:
  schemas:
    CountRequest:
      type: object
      description: >-
        Request parameters for counting documents in a collection. Use this to
        get the total number of documents matching specific criteria.
      required:
        - collection
        - query
      properties:
        args:
          $ref: '#/components/schemas/AnyValue'
          description: >-
            Optional parameters to use in the parameterized query, providing
            safe value substitution
        collection:
          type: string
          description: The name of the collection to count documents in
        query:
          type: string
          description: >-
            A query expression that filters which documents to count. Use
            parameterized values with :param syntax for better security and
            performance
      example:
        collection: people
        query: favoriteBook.title == 'The Great Gatsby'
    CountResponse:
      type: object
      description: >-
        Response from a count operation, providing both the count and
        transaction ID for consistency tracking.
      required:
        - count
        - txnId
      properties:
        count:
          type: integer
          format: int64
          description: The total number of documents that matched the query criteria
        txnId:
          type: integer
          format: int64
          description: >-
            The transaction ID at which this count was performed, useful for
            consistency checks
      example:
        count: 100
        txnId: 9000
    RpcError:
      type: object
      description: Error response returned when an API request fails
      required:
        - message
      properties:
        message:
          type: string
          description: Human-readable description of what went wrong
      example:
        message: Some kind of human readable description of the error
    AnyValue: {}
  securitySchemes:
    api_key_or_jwt_token:
      type: http
      scheme: bearer
      bearerFormat: API Key or JWT
      description: >-
        Authentication using either an API key or JWT token in the Authorization
        header

````