Collection Documents

Aggregate Documents

Changed on

This endpoint performs aggregation operations on documents in a collection.

**Aggregation Framework**: Provides MongoDB-style aggregation operations:
- GROUP BY: Group documents by one or more fields
- Aggregations: COUNT, SUM, AVG, MIN, MAX, COUNT_DISTINCT, etc.
- Date Operations: Truncate or extract date parts for time-series analysis
- Filtering: Pre-aggregation filters (WHERE) and post-aggregation filters (HAVING)
- Sorting & Limiting: Control result ordering and size

**Counting documents that match a filter**: omit group_by and send only count
aggregations, for example
`{"aggregations": [{"function": "count", "alias": "total"}], "filters": {...}}`.
The response holds one row whose metrics carry the exact number of matching
documents (an empty `group`). This is the same count documents/list returns in
`pagination.total` when you send `"include_total": true`. Any other
aggregation needs at least one group_by field.

**Use Cases**:
- Count documents by feature type or collection
- Calculate daily/monthly processing statistics
- Analyze feature distributions and confidence scores
- Generate reports with multiple metrics

**Group values**: a group_by value comes back as a JSON number or boolean when the
namespace declares a payload index of type integer, float or bool on the field. Any
other field, including an undeclared numeric one, groups as strings.

**Note**: This endpoint works with both MongoDB and the vector store using the same interface.
The system automatically selects the appropriate aggregation provider based on
the underlying metadata store.
post/v1/collections/{collection_identifier}/documents/aggregate

Request

Path parameters

collection_identifierstring required

The unique identifier of the collection.

Request body

filtersobject nullable

Pre-aggregation filters to apply to source data. OPTIONAL, filters data before grouping. Uses same syntax as standard query filters. Applied before GROUP BY.

unwindstring nullable

Array field to unwind before aggregation. OPTIONAL, creates one document per array element. Useful for aggregating over array contents. Example: 'blobs' to analyze each blob separately.

sort_bystring nullable

Field to sort results by. OPTIONAL, can be group_by field or aggregation alias. Defaults to no specific order. Use with sort_direction to control order.

sort_directionstring

Sort direction. OPTIONAL, defaults to 'desc' (descending). Valid values: 'asc' (ascending), 'desc' (descending). Used with sort_by field.

limitinteger nullable

Maximum number of results to return. OPTIONAL, no limit if not specified. Applied after sorting. Useful for 'top N' queries.

Example request

{
  "aggregations": [
    {
      "alias": "total",
      "function": "count"
    }
  ],
  "description": "Count documents matching a filter",
  "filters": {
    "metadata.status": "active"
  }
}

Response

Successful Response

total_groupsinteger required

Total number of unique groups returned.

query_infoobject

Additional information about the query execution. May include collection info, pipeline stages, execution time, etc.

Example response

{
  "query_info": {
    "collection_id": "my-collection",
    "execution_time_ms": 32,
    "pipeline_stages": 4
  },
  "results": [
    {
      "group": {
        "collection": "coll_123"
      },
      "metrics": {
        "total": 523
      }
    },
    {
      "group": {
        "collection": "coll_456"
      },
      "metrics": {
        "total": 187
      }
    }
  ],
  "total_groups": 2
}

Changes

    • info

      the request property became optional

    • warning

      removed the optional property from the response with the status

    • info

      added the non-success response with the status

    • info

      added the non-success response with the status

    • info

      added the optional property to the response with the status

    • info

      added the required property to the response with the status

    • info

      added the required property to the response with the status