Skip to content
This page View Markdown Open in ChatGPT Open in Claude

Aggregate

The code in every language

Each example is the inside of main, written in every language API Maker runs : pick yours in the tabs, and every page shows it from then on. The types a call takes (GetAll, DeepItem…) are those of am : from am import GetAll in Python, use am::GetAll; in Rust, import am.GetAll; in Java and Kotlin ; PHP and C# have them without an import, Go names them am.GetAll.

g.sys.db.aggregate calls the aggregate schema API of any table from your code, with the same headers and query parameters as over HTTP. It returns the data. The whole envelope (success, statusCode, data, totalCount, errors…) comes back when true is passed as the second argument in TypeScript, and through full in the other languages : g.sys.db.full (g.Sys.Db.Full in C# and Go, $g->sys->db->full in PHP).

Aggregation runs on MongoDB. aggregateQuery is a pipeline of MongoDB : every stage and operator of the MongoDB manual works, so this page shows three common shapes.

Group, filter and count : $group, $match

MongoDB Basic aggregation
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb", database: "inventory", collection: "customers",
    aggregateQuery: [
        {
            "$group": {
                "_id": "$_id",
                "total": {
                    "$sum": "$customer_id"
                }
            }
        },
        {
            "$match": {
                "total": {
                    "$gt": 1
                }
            }
        },
        {
            "$sort": {
                "total": -1
            }
        },
        {
            "$count": "CountValue"
        }
    ]
});
MongoDB Basic aggregation
aggregate = await g.sys.db.aggregate(
    'mongodb', 'inventory', 'customers',
    aggregate_query=[
        {
            '$group': {
                '_id': '$_id',
                'total': {
                    '$sum': '$customer_id',
                },
            },
        },
        {
            '$match': {
                'total': {
                    '$gt': 1,
                },
            },
        },
        {
            '$sort': {
                'total': -1,
            },
        },
        {
            '$count': 'CountValue',
        },
    ],
)
MongoDB Basic aggregation
$aggregate = $g->sys->db->aggregate(
    'mongodb', 'inventory', 'customers',
    aggregateQuery: [
        [
            '$group' => [
                '_id' => '$_id',
                'total' => [
                    '$sum' => '$customer_id',
                ],
            ],
        ],
        [
            '$match' => [
                'total' => [
                    '$gt' => 1,
                ],
            ],
        ],
        [
            '$sort' => [
                'total' => -1,
            ],
        ],
        [
            '$count' => 'CountValue',
        ],
    ],
);
MongoDB Basic aggregation
var aggregate = await g.Sys.Db.Aggregate(new Aggregate("mongodb", "inventory", "customers")
{
    AggregateQuery = [
        new JsonObject
        {
            ["$group"] = new JsonObject
            {
                ["_id"] = "$_id",
                ["total"] = new JsonObject
                {
                    ["$sum"] = "$customer_id",
                },
            },
        },
        new JsonObject
        {
            ["$match"] = new JsonObject
            {
                ["total"] = new JsonObject
                {
                    ["$gt"] = 1,
                },
            },
        },
        new JsonObject
        {
            ["$sort"] = new JsonObject
            {
                ["total"] = -1,
            },
        },
        new JsonObject
        {
            ["$count"] = "CountValue",
        },
    ],
});
MongoDB Basic aggregation
aggregate, err := g.Sys.Db.Aggregate(am.Aggregate{
    Instance: "mongodb", Database: "inventory", Collection: "customers",
    AggregateQuery: []any{
        am.M{
            "$group": am.M{
                "_id": "$_id",
                "total": am.M{
                    "$sum": "$customer_id",
                },
            },
        },
        am.M{
            "$match": am.M{
                "total": am.M{
                    "$gt": 1,
                },
            },
        },
        am.M{
            "$sort": am.M{
                "total": -1,
            },
        },
        am.M{
            "$count": "CountValue",
        },
    },
})
if err != nil {
    return nil, err
}
MongoDB Basic aggregation
let aggregate = g.sys.db.aggregate(
    Aggregate::new("mongodb", "inventory", "customers")
        .aggregate_query([
            json!({
                "$group": {
                    "_id": "$_id",
                    "total": {
                        "$sum": "$customer_id",
                    },
                },
            }),
            json!({
                "$match": {
                    "total": {
                        "$gt": 1,
                    },
                },
            }),
            json!({
                "$sort": {
                    "total": -1,
                },
            }),
            json!({
                "$count": "CountValue",
            }),
        ]),
)?;
MongoDB Basic aggregation
var aggregate = g.sys.db.aggregate(new Aggregate("mongodb", "inventory", "customers")
    .aggregateQuery(
        Map.of(
            "$group", Map.of(
                "_id", "$_id",
                "total", Map.of(
                    "$sum", "$customer_id"
                )
            )
        ),
        Map.of(
            "$match", Map.of(
                "total", Map.of(
                    "$gt", 1
                )
            )
        ),
        Map.of(
            "$sort", Map.of(
                "total", -1
            )
        ),
        Map.of(
            "$count", "CountValue"
        )
    ));
MongoDB Basic aggregation
val aggregate = g.sys.db.aggregate(
    Aggregate("mongodb", "inventory", "customers")
        .aggregateQuery(
            mapOf(
                "\$group" to mapOf(
                    "_id" to "\$_id",
                    "total" to mapOf(
                        "\$sum" to "\$customer_id",
                    ),
                ),
            ),
            mapOf(
                "\$match" to mapOf(
                    "total" to mapOf(
                        "\$gt" to 1,
                    ),
                ),
            ),
            mapOf(
                "\$sort" to mapOf(
                    "total" to -1,
                ),
            ),
            mapOf(
                "\$count" to "CountValue",
            ),
        ),
)

Several groupings at once : $facet and $bucket

MongoDB $facet with multiple $bucket
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb", database: "inventory", collection: "customers",
    aggregateQuery:[
        {
            "$facet": {
                "price": [
                    {
                        "$bucket": {
                            "groupBy": "$price",
                            "boundaries": [10000,20000,30000,40000,50000,60000,70000,80000],
                            "default": "Other",
                            "output": {
                                "count": {
                                    "$sum": 1
                                },
                                "product_details": {
                                    "$push": {
                                        "name": "$name",
                                        "price": "$price"
                                    }
                                }
                            }
                        }
                    }
                ],
                "categories": [
                    {
                        "$bucket": {
                            "groupBy": "$category_id",
                            "boundaries": [10000,20000,30000,40000,50000,60000,70000,80000],
                            "default": "Other",
                            "output": {
                                "count": {
                                    "$sum": 1
                                },
                                "product_details": {
                                    "$push": {
                                        "name": "$name",
                                        "id": "$id",
                                        "category_id": "$category_id"
                                    }
                                }
                            }
                        }
                    }
                ]
            }
        }
    ]
});
MongoDB $facet with multiple $bucket
aggregate = await g.sys.db.aggregate(
    'mongodb', 'inventory', 'customers',
    aggregate_query=[
        {
            '$facet': {
                'price': [
                    {
                        '$bucket': {
                            'groupBy': '$price',
                            'boundaries': [10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000],
                            'default': 'Other',
                            'output': {
                                'count': {
                                    '$sum': 1,
                                },
                                'product_details': {
                                    '$push': {
                                        'name': '$name',
                                        'price': '$price',
                                    },
                                },
                            },
                        },
                    },
                ],
                'categories': [
                    {
                        '$bucket': {
                            'groupBy': '$category_id',
                            'boundaries': [10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000],
                            'default': 'Other',
                            'output': {
                                'count': {
                                    '$sum': 1,
                                },
                                'product_details': {
                                    '$push': {
                                        'name': '$name',
                                        'id': '$id',
                                        'category_id': '$category_id',
                                    },
                                },
                            },
                        },
                    },
                ],
            },
        },
    ],
)
MongoDB $facet with multiple $bucket
$aggregate = $g->sys->db->aggregate(
    'mongodb', 'inventory', 'customers',
    aggregateQuery: [
        [
            '$facet' => [
                'price' => [
                    [
                        '$bucket' => [
                            'groupBy' => '$price',
                            'boundaries' => [10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000],
                            'default' => 'Other',
                            'output' => [
                                'count' => [
                                    '$sum' => 1,
                                ],
                                'product_details' => [
                                    '$push' => [
                                        'name' => '$name',
                                        'price' => '$price',
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
                'categories' => [
                    [
                        '$bucket' => [
                            'groupBy' => '$category_id',
                            'boundaries' => [10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000],
                            'default' => 'Other',
                            'output' => [
                                'count' => [
                                    '$sum' => 1,
                                ],
                                'product_details' => [
                                    '$push' => [
                                        'name' => '$name',
                                        'id' => '$id',
                                        'category_id' => '$category_id',
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
);
MongoDB $facet with multiple $bucket
var aggregate = await g.Sys.Db.Aggregate(new Aggregate("mongodb", "inventory", "customers")
{
    AggregateQuery = [
        new JsonObject
        {
            ["$facet"] = new JsonObject
            {
                ["price"] = new JsonArray
                {
                    new JsonObject
                    {
                        ["$bucket"] = new JsonObject
                        {
                            ["groupBy"] = "$price",
                            ["boundaries"] = new JsonArray { 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000 },
                            ["default"] = "Other",
                            ["output"] = new JsonObject
                            {
                                ["count"] = new JsonObject
                                {
                                    ["$sum"] = 1,
                                },
                                ["product_details"] = new JsonObject
                                {
                                    ["$push"] = new JsonObject
                                    {
                                        ["name"] = "$name",
                                        ["price"] = "$price",
                                    },
                                },
                            },
                        },
                    },
                },
                ["categories"] = new JsonArray
                {
                    new JsonObject
                    {
                        ["$bucket"] = new JsonObject
                        {
                            ["groupBy"] = "$category_id",
                            ["boundaries"] = new JsonArray { 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000 },
                            ["default"] = "Other",
                            ["output"] = new JsonObject
                            {
                                ["count"] = new JsonObject
                                {
                                    ["$sum"] = 1,
                                },
                                ["product_details"] = new JsonObject
                                {
                                    ["$push"] = new JsonObject
                                    {
                                        ["name"] = "$name",
                                        ["id"] = "$id",
                                        ["category_id"] = "$category_id",
                                    },
                                },
                            },
                        },
                    },
                },
            },
        },
    ],
});
MongoDB $facet with multiple $bucket
aggregate, err := g.Sys.Db.Aggregate(am.Aggregate{
    Instance: "mongodb", Database: "inventory", Collection: "customers",
    AggregateQuery: []any{
        am.M{
            "$facet": am.M{
                "price": []any{
                    am.M{
                        "$bucket": am.M{
                            "groupBy":    "$price",
                            "boundaries": []any{10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000},
                            "default":    "Other",
                            "output": am.M{
                                "count": am.M{
                                    "$sum": 1,
                                },
                                "product_details": am.M{
                                    "$push": am.M{
                                        "name":  "$name",
                                        "price": "$price",
                                    },
                                },
                            },
                        },
                    },
                },
                "categories": []any{
                    am.M{
                        "$bucket": am.M{
                            "groupBy":    "$category_id",
                            "boundaries": []any{10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000},
                            "default":    "Other",
                            "output": am.M{
                                "count": am.M{
                                    "$sum": 1,
                                },
                                "product_details": am.M{
                                    "$push": am.M{
                                        "name":        "$name",
                                        "id":          "$id",
                                        "category_id": "$category_id",
                                    },
                                },
                            },
                        },
                    },
                },
            },
        },
    },
})
if err != nil {
    return nil, err
}
MongoDB $facet with multiple $bucket
let aggregate = g.sys.db.aggregate(
    Aggregate::new("mongodb", "inventory", "customers")
        .aggregate_query([
            json!({
                "$facet": {
                    "price": [
                        {
                            "$bucket": {
                                "groupBy": "$price",
                                "boundaries": [10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000],
                                "default": "Other",
                                "output": {
                                    "count": {
                                        "$sum": 1,
                                    },
                                    "product_details": {
                                        "$push": {
                                            "name": "$name",
                                            "price": "$price",
                                        },
                                    },
                                },
                            },
                        },
                    ],
                    "categories": [
                        {
                            "$bucket": {
                                "groupBy": "$category_id",
                                "boundaries": [10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000],
                                "default": "Other",
                                "output": {
                                    "count": {
                                        "$sum": 1,
                                    },
                                    "product_details": {
                                        "$push": {
                                            "name": "$name",
                                            "id": "$id",
                                            "category_id": "$category_id",
                                        },
                                    },
                                },
                            },
                        },
                    ],
                },
            }),
        ]),
)?;
MongoDB $facet with multiple $bucket
var aggregate = g.sys.db.aggregate(new Aggregate("mongodb", "inventory", "customers")
    .aggregateQuery(
        Map.of(
            "$facet", Map.of(
                "price", List.of(
                    Map.of(
                        "$bucket", Map.of(
                            "groupBy", "$price",
                            "boundaries", List.of(10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000),
                            "default", "Other",
                            "output", Map.of(
                                "count", Map.of(
                                    "$sum", 1
                                ),
                                "product_details", Map.of(
                                    "$push", Map.of(
                                        "name", "$name",
                                        "price", "$price"
                                    )
                                )
                            )
                        )
                    )
                ),
                "categories", List.of(
                    Map.of(
                        "$bucket", Map.of(
                            "groupBy", "$category_id",
                            "boundaries", List.of(10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000),
                            "default", "Other",
                            "output", Map.of(
                                "count", Map.of(
                                    "$sum", 1
                                ),
                                "product_details", Map.of(
                                    "$push", Map.of(
                                        "name", "$name",
                                        "id", "$id",
                                        "category_id", "$category_id"
                                    )
                                )
                            )
                        )
                    )
                )
            )
        )
    ));
MongoDB $facet with multiple $bucket
val aggregate = g.sys.db.aggregate(
    Aggregate("mongodb", "inventory", "customers")
        .aggregateQuery(
            mapOf(
                "\$facet" to mapOf(
                    "price" to listOf(
                        mapOf(
                            "\$bucket" to mapOf(
                                "groupBy" to "\$price",
                                "boundaries" to listOf(10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000),
                                "default" to "Other",
                                "output" to mapOf(
                                    "count" to mapOf(
                                        "\$sum" to 1,
                                    ),
                                    "product_details" to mapOf(
                                        "\$push" to mapOf(
                                            "name" to "\$name",
                                            "price" to "\$price",
                                        ),
                                    ),
                                ),
                            ),
                        ),
                    ),
                    "categories" to listOf(
                        mapOf(
                            "\$bucket" to mapOf(
                                "groupBy" to "\$category_id",
                                "boundaries" to listOf(10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000),
                                "default" to "Other",
                                "output" to mapOf(
                                    "count" to mapOf(
                                        "\$sum" to 1,
                                    ),
                                    "product_details" to mapOf(
                                        "\$push" to mapOf(
                                            "name" to "\$name",
                                            "id" to "\$id",
                                            "category_id" to "\$category_id",
                                        ),
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
)

Join another collection : $lookup

MongoDB $lookup
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb", database: "inventory", collection: "customers",
    aggregateQuery:[
        {
            "$lookup": {
                "from": "products",
                "localField": "customer_id",
                "foreignField": "owner_id",
                "as": "customer_data"
            }
        }
    ]
});
MongoDB $lookup
aggregate = await g.sys.db.aggregate(
    'mongodb', 'inventory', 'customers',
    aggregate_query=[
        {
            '$lookup': {
                'from': 'products',
                'localField': 'customer_id',
                'foreignField': 'owner_id',
                'as': 'customer_data',
            },
        },
    ],
)
MongoDB $lookup
$aggregate = $g->sys->db->aggregate(
    'mongodb', 'inventory', 'customers',
    aggregateQuery: [
        [
            '$lookup' => [
                'from' => 'products',
                'localField' => 'customer_id',
                'foreignField' => 'owner_id',
                'as' => 'customer_data',
            ],
        ],
    ],
);
MongoDB $lookup
var aggregate = await g.Sys.Db.Aggregate(new Aggregate("mongodb", "inventory", "customers")
{
    AggregateQuery = [
        new JsonObject
        {
            ["$lookup"] = new JsonObject
            {
                ["from"] = "products",
                ["localField"] = "customer_id",
                ["foreignField"] = "owner_id",
                ["as"] = "customer_data",
            },
        },
    ],
});
MongoDB $lookup
aggregate, err := g.Sys.Db.Aggregate(am.Aggregate{
    Instance: "mongodb", Database: "inventory", Collection: "customers",
    AggregateQuery: []any{
        am.M{
            "$lookup": am.M{
                "from":         "products",
                "localField":   "customer_id",
                "foreignField": "owner_id",
                "as":           "customer_data",
            },
        },
    },
})
if err != nil {
    return nil, err
}
MongoDB $lookup
let aggregate = g.sys.db.aggregate(
    Aggregate::new("mongodb", "inventory", "customers")
        .aggregate_query([
            json!({
                "$lookup": {
                    "from": "products",
                    "localField": "customer_id",
                    "foreignField": "owner_id",
                    "as": "customer_data",
                },
            }),
        ]),
)?;
MongoDB $lookup
var aggregate = g.sys.db.aggregate(new Aggregate("mongodb", "inventory", "customers")
    .aggregateQuery(
        Map.of(
            "$lookup", Map.of(
                "from", "products",
                "localField", "customer_id",
                "foreignField", "owner_id",
                "as", "customer_data"
            )
        )
    ));
MongoDB $lookup
val aggregate = g.sys.db.aggregate(
    Aggregate("mongodb", "inventory", "customers")
        .aggregateQuery(
            mapOf(
                "\$lookup" to mapOf(
                    "from" to "products",
                    "localField" to "customer_id",
                    "foreignField" to "owner_id",
                    "as" to "customer_data",
                ),
            ),
        ),
)

Multi-tenant

  • instance: "crm::acme" works on the database of the tenant acme of the multi-tenant instance crm.
  • With instance: "crm", the call works on the database of the tenant of the request: a custom API called with the x-am-tenant-username header passes it on to the APIs it calls. A request without a tenant works on the structure database.
Aggregate the customers of the tenant acme (MongoDB)
1
2
3
4
5
6
7
8
9
let aggregate = await g.sys.db.aggregate({
    instance: "crm::acme", database: "crm", collection: "customers",
    aggregateQuery: [{
        "$group": {
            "_id": "$city",
            "customers": { "$sum": 1 }
        }
    }]
});
Aggregate the customers of the tenant acme (MongoDB)
aggregate = await g.sys.db.aggregate(
    'crm::acme', 'crm', 'customers',
    aggregate_query=[
        {
            '$group': {
                '_id': '$city',
                'customers': {'$sum': 1},
            },
        },
    ],
)
Aggregate the customers of the tenant acme (MongoDB)
$aggregate = $g->sys->db->aggregate(
    'crm::acme', 'crm', 'customers',
    aggregateQuery: [
        [
            '$group' => [
                '_id' => '$city',
                'customers' => ['$sum' => 1],
            ],
        ],
    ],
);
Aggregate the customers of the tenant acme (MongoDB)
var aggregate = await g.Sys.Db.Aggregate(new Aggregate("crm::acme", "crm", "customers")
{
    AggregateQuery = [
        new JsonObject
        {
            ["$group"] = new JsonObject
            {
                ["_id"] = "$city",
                ["customers"] = new JsonObject { ["$sum"] = 1 },
            },
        },
    ],
});
Aggregate the customers of the tenant acme (MongoDB)
aggregate, err := g.Sys.Db.Aggregate(am.Aggregate{
    Instance: "crm::acme", Database: "crm", Collection: "customers",
    AggregateQuery: []any{
        am.M{
            "$group": am.M{
                "_id":       "$city",
                "customers": am.M{"$sum": 1},
            },
        },
    },
})
if err != nil {
    return nil, err
}
Aggregate the customers of the tenant acme (MongoDB)
let aggregate = g.sys.db.aggregate(
    Aggregate::new("crm::acme", "crm", "customers")
        .aggregate_query([
            json!({
                "$group": {
                    "_id": "$city",
                    "customers": { "$sum": 1 },
                },
            }),
        ]),
)?;
Aggregate the customers of the tenant acme (MongoDB)
1
2
3
4
5
6
7
8
9
var aggregate = g.sys.db.aggregate(new Aggregate("crm::acme", "crm", "customers")
    .aggregateQuery(
        Map.of(
            "$group", Map.of(
                "_id", "$city",
                "customers", Map.of("$sum", 1)
            )
        )
    ));
Aggregate the customers of the tenant acme (MongoDB)
val aggregate = g.sys.db.aggregate(
    Aggregate("crm::acme", "crm", "customers")
        .aggregateQuery(
            mapOf(
                "\$group" to mapOf(
                    "_id" to "\$city",
                    "customers" to mapOf("\$sum" to 1),
                ),
            ),
        ),
)

Without a schema

g.sys.db.gen.aggregateGen takes the same arguments and calls the aggregate generated API : no schema, so the data is read and written as it is in the table, without the conversions, validations and defaults of the schema.

Language The call
TypeScript g.sys.db.gen.aggregateGen(…)
Python g.sys.db.gen.aggregate_gen(…)
PHP $g->sys->db->gen->aggregateGen(…)
C#, Go g.Sys.Db.Gen.AggregateGen(…)
Rust g.sys.db.aggregate_gen(Aggregate::new(…))
Java, Kotlin g.sys.db.gen.aggregateGen(…)