Skip to main content

DB Viewer - Aggregation Query

Endpoint

/o/db?db=countly&collection=members&aggregation=[{"$match":{}},{"$group":{"_id":"$role","count":{"$sum":1}}}]

Overview

Executes a MongoDB aggregation pipeline for the selected collection.

Authentication

Countly API supports three authentication methods:

  1. api_key=YOUR_API_KEY
  2. auth_token=YOUR_AUTH_TOKEN
  3. countly-token: YOUR_AUTH_TOKEN

Permissions

Requires DB Viewer access (dbviewer read right for app-scoped users).

Request Parameters

ParameterTypeRequiredDescription
api_keyStringConditionalRequired if auth_token is not provided.
auth_tokenStringConditionalRequired if api_key is not provided.
db / dbsStringYesMongoDB database name.
collectionStringYesCollection name.
aggregationJSON String (Array)YesAggregation pipeline array.
iDisplayLengthNumberNoIf set, endpoint appends $limit stage with this value.
sEchoStringNoEcho value returned in result envelope.
save_reportBoolean/StringNoIf truthy, forces long-task storage path.
report_nameStringNoTask/report display name.
report_descStringNoTask/report description.
period_descStringNoTask/report period description.
globalBoolean/StringNoGlobal visibility flag for saved task report.
autoRefreshBoolean/StringNoAuto-refresh flag for task report.
manually_createBoolean/StringNoMarks task as manually created.

Configuration Impact

SettingDefaultAffectsUser-visible impact
api.request_thresholdServer-definedLong-task thresholdHeavy aggregation requests switch to task mode and return task_id instead of immediate results.

Response

Success Response (direct output)

{
"sEcho": "1",
"iTotalRecords": 0,
"iTotalDisplayRecords": 0,
"aaData": [
{"_id": "admin", "count": 12}
],
"removed": {}
}

Success Response (task mode)

{
"task_id": "65fca8f79f8a0f0012f3c112"
}

Response Fields

FieldTypeDescription
task_idStringPresent when request is queued or matched to existing running task.
aaDataArrayAggregation results in direct output mode.
removedObjectFor restricted users, lists removed non-whitelisted pipeline stages.
sEchoStringEcho value from request.

Error Responses

  • 500
{
"result": "Aggregation object is not valid."
}
  • 500
{
"result": "The aggregation pipeline must be of the type array"
}
  • 401
{
"result": "User does not have right tot view this colleciton"
}

Behavior/Processing

  • Parses aggregation as EJSON array.
  • For non-admin users, pipeline stages are filtered through an allowlist and removed stages are returned in removed.
  • For non-admin users querying events_data/drill_events, app-level base filter is prepended as $match.
  • For members, sensitive fields are projected out in pipeline path.
  • For auth_tokens, _id is redacted in pipeline path.
  • Execution is managed through task manager; responses can be immediate output or task_id based on threshold/running task conditions.

Database Collections

This endpoint reads from the selected MongoDB collection and may write task artifacts via task manager/GridFS when queued.

Examples

Run aggregation directly

/o/db?api_key=YOUR_API_KEY&db=countly&collection=members&aggregation=[{"$group":{"_id":"$global_admin","count":{"$sum":1}}}]

Run and force task/report mode

/o/db?api_key=YOUR_API_KEY&db=countly&collection=members&aggregation=[{"$group":{"_id":"$role","count":{"$sum":1}}}]&save_report=true&report_name=RoleSummary

Last Updated

2026-03-07