Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.ub.bitbros.in/llms.txt

Use this file to discover all available pages before exploring further.

POST /api/data/:collectionName/aggregate

Runs a MongoDB aggregation pipeline on the specified collection. This allows for complex data processing, grouping, and transformations.

Required header

x-api-key: your pk_live_… or sk_live_… key.

Path parameters

collectionName
string
required
The name of the collection to aggregate.

Query parameters

include_deleted
boolean
If true, includes soft-deleted documents (where isDeleted: true) in the aggregation. Defaults to false.

Request body

pipeline
array
required
An array of aggregation stages following the standard MongoDB aggregation pipeline syntax.
For security reasons, certain stages like $out and $merge are blocked.

RLS behavior

If Row-Level Security (RLS) is enabled, your backend automatically injects a $match stage at the beginning of your pipeline (or after $geoNear / $search if present). This ensures users only aggregate data they have access to.

Response fields

success
boolean
true when the aggregation was executed successfully.
data
array
The results of the aggregation pipeline.
message
string
Human-readable status message.

Code examples

const res = await fetch(
  'https://api.ub.bitbros.in/api/data/orders/aggregate',
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'x-api-key': 'pk_live_YOUR_KEY'
    },
    body: JSON.stringify({
      pipeline: [
        { $match: { status: 'completed' } },
        { $group: { _id: '$customerId', totalAmount: { $sum: '$amount' } } },
        { $sort: { totalAmount: -1 } }
      ]
    })
  }
);

const { success, data } = await res.json();

Success response

{
  "success": true,
  "data": [
    { "_id": "electronics", "count": 42 },
    { "_id": "books", "count": 12 }
  ],
  "message": "Aggregation executed successfully."
}

Errors

StatusCause
400 Bad RequestInvalid pipeline syntax or blocked stage used
401 UnauthorizedMissing or invalid API key
404 Not FoundCollection does not exist