Skip to content

Aggregate

MongoDB Simple aggregate

MongoDB Simple aggregate
1
2
3
4
5
6
7
8
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[{
        "$limit": 2
    }]
});

MongoDB headers

MongoDB headers
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[{
        "$limit": 2
    }],
    headers: {
        "x-am-authorization": "TOKEN"
    }
});

MongoDB Basic aggregation

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 $project stage

MongoDB $project stage
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "full_name": {
                    "$toLower": "$first_name"
                }
            }
        }
    ]
});

MongoDB $addFields

MongoDB $addFields
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$addFields": {
                "full_name": {
                    "$concat": [
                        "$first_name",
                        " ",
                        "$last_name"
                    ]
                }
            }
        }
    ]
});

MongoDB $bucket

MongoDB $bucket
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$bucket": {
                "groupBy": "$price",
                "boundaries": [10000,20000,30000,40000,50000],
                "default": "Other",
                "output": {
                    "count": {
                        "$sum": 1
                    },
                    "product_details": {
                        "$push": {
                            "name": "$name",
                            "price": "$price"
                        }
                    }
                }
            }
        }
    ]
});

MongoDB $facet with multiple $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 $collStats, latencyStats > histograms, storageStats, count

MongoDB $collStats, latencyStats > histograms, storageStats, count
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$collStats": {
                "latencyStats": {
                    "histograms": true
                },
                "storageStats": {},
                "count": {}
            }
        }
    ]
});

MongoDB $collStats, latencyStats > histograms, storageStats, count

MongoDB $collStats, latencyStats > histograms, storageStats, count
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$collStats": {
                "latencyStats": {
                    "histograms": true
                },
                "storageStats": {},
                "count": {}
            }
        }
    ]
});

MongoDB $count

MongoDB $count
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$match": {
                "price": {
                    "$gt": 10000
                }
            }
        },
        {
            "$count": "price_gt_10000"
        }
    ]
});

MongoDB $limit

MongoDB $limit
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$limit": 2
        }
    ]
});

MongoDB $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 $match

MongoDB $match
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$match": {
                "first_name": "Bob"
            }
        }
    ]
});

MongoDB $redact, $cond, if, then, else, $$DESCEND, $$PRUNE

MongoDB $redact, $cond, if, then, else, $$DESCEND, $$PRUNE
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$redact": {
                "$cond": {
                    "if": {
                        "$eq": [
                            "$first_name",
                            "JOHNNY"
                        ]
                    },
                    "then": "$$DESCEND",
                    "else": "$$PRUNE"
                }
            }
        }
    ]
});

MongoDB $replaceRoot, newRoot

MongoDB $replaceRoot, newRoot
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$replaceRoot": {
                "newRoot": {
                    "full_name": {
                        "$concat": [
                            "$first_name",
                            " ",
                            "$last_name"
                        ]
                    }
                }
            }
        }
    ]
});

MongoDB $sample

MongoDB $sample
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$sample": {
                "size": 2
            }
        }
    ]
});

MongoDB $skip

MongoDB $skip
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$skip": 2
        }
    ]
});

MongoDB $sort

ascending

MongoDB $sort
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$sort": {
                "customer_id": 1
            }
        }
    ]
});

descending

MongoDB $sort
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$sort": {
                "customer_id": -1
            }
        }
    ]
});

MongoDB $unwind, $sortByCount

MongoDB $unwind, $sortByCount
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$unwind": "$ages"
        },
        {
            "$sortByCount": "$ages"
        }
    ]
});

MongoDB $abs, $subtract

MongoDB $abs, $subtract
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "Grand_Total": {
                    "$abs": {
                        "$subtract": [
                            "$total",
                            "$tax"
                        ]
                    }
                }
            }
        }
    ]
});

MongoDB $add

MongoDB $add
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "Grand_Total": {
                    "$add": [
                        "$price",
                        "$category_id"
                    ]
                }
            }
        }
    ]
});

MongoDB $ceil, $floor

MongoDB $ceil, $floor
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "ceilingValue": {
                    "$ceil": "$price"
                },
                "floorValue": {
                    "$floor": "$price"
                },
                "id": 1
            }
        }
    ]
});

MongoDB $divide

MongoDB $divide
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "pricePercentage": {
                    "$divide": [
                        "$price",
                        100
                    ]
                },
                "id": 1
            }
        }
    ]
});

MongoDB $multiply

MongoDB $multiply
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "Grand_Total": {
                    "$multiply": [
                        "$price",
                        "$quantity"
                    ]
                },
                "id": 1
            }
        }
    ]
});

MongoDB $arrayElemAt

MongoDB $arrayElemAt
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
          "$project": {
            "first": {
              "$arrayElemAt": [
                "$attribuites.color",
                1
              ]
            }
          }
        }
    ]
});

MongoDB $filter

MongoDB $filter
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
          "$project": {
            "ages": {
              "$filter": {
                "input": "$ages",
                "as": "age",
                "cond": {
                  "$gte": [
                    "$$age.age",
                    50
                  ]
                }
              }
            }
          }
        }
    ]
});

MongoDB $in

MongoDB $in
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "customer_id": "$customer_id",
                "TravelingHobby": {
                    "$in": [
                        "traveling",
                        "$hobbies"
                    ]
                }
            }
        }
    ]
});

MongoDB $indexOfArray

MongoDB $indexOfArray
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "customer_id": "$customer_id",
                "index": {
                    "$indexOfArray": [
                        "$ages.birth_year",
                        1971
                    ]
                }
            }
        }
    ]
});

MongoDB $isArray

MongoDB $isArray
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "isHobbyCricket": {
                    "$cond": {
                        "if": {
                            "$isArray": "$hobbies"
                        },
                        "then": {
                            "$in": [
                                "traveling",
                                "$hobbies"
                            ]
                        },
                        "else": "One or more fields is not an array."
                    }
                }
            }
        }
    ]
});

MongoDB $map

MongoDB $map
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "integerValues": {
                    "$map": {
                        "input": "$distances",
                        "as": "decimalValue",
                        "in": {
                            "$trunc": "$$decimalValue"
                        }
                    }
                }
            }
        }
    ]
});

MongoDB $reverseArray

MongoDB $reverseArray
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "reverseDistances": {
                    "$reverseArray": "$distances"
                }
            }
        }
    ]
});

MongoDB $size

MongoDB $size
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "customer_id": 1,
                "address": 1,
                "numberOfHobbies": {
                    "$cond": {
                        "if": {
                            "$isArray": "$hobbies"
                        },
                        "then": {
                            "$size": "$hobbies"
                        },
                        "else": "NA"
                    }
                }
            }
        }
    ]
});

MongoDB $and

MongoDB $and
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": "$id",
                "result": {
                    "$and": [
                        {
                            "$gt": [
                                "$price",
                                60000
                            ]
                        },
                        {
                            "$lt": [
                                "$category_id",
                                45000
                            ]
                        }
                    ]
                }
            }
        }
    ]
});

MongoDB $not

MongoDB $not
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": "$id",
                "result": {
                    "$not": [
                        {
                            "$gt": [
                                "$price",
                                30000
                            ]
                        }
                    ]
                }
            }
        }
    ]
});

MongoDB $or

MongoDB $or
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": "$id",
                "result": {
                    "$or": [
                        {
                            "$gt": [
                                "$price",
                                60000
                            ]
                        },
                        {
                            "$lt": [
                                "$category_id",
                                1000
                            ]
                        }
                    ]
                }
            }
        }
    ]
});

MongoDB $cmp

MongoDB $cmp
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "comparePrice": {
                    "$cmp": [
                        "$price",
                        42187
                    ]
                },
                "_id": 1
            }
        }
    ]
});

MongoDB $eq

MongoDB $eq
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "equalPrice": {
                    "$eq": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});

MongoDB $gt

MongoDB $gt
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "graterPrice": {
                    "$gt": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});

MongoDB $gte

MongoDB $gte
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "gePrice": {
                    "$gte": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});

MongoDB $lt

MongoDB $lt
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "graterPrice": {
                    "$lt": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});

MongoDB $lte

MongoDB $lte
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "gePrice": {
                    "$lte": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});

MongoDB $ne

MongoDB $ne
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "nePrice": {
                    "$ne": [
                        "$price",
                        42187
                    ]
                },
                "_id": 0
            }
        }
    ]
});

MongoDB $cond

MongoDB $cond
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "price": 1,
                "discount": {
                    "$cond": {
                        "if": {
                            "$gt": [
                                "$price",
                                50000
                            ]
                        },
                        "then": 30,
                        "else": 10
                    }
                },
                "_id": 0
            }
        }
    ]
});

MongoDB $ifNull

MongoDB $ifNull
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "price": 1,
                "shipping": {
                    "$ifNull": [
                        "$shipping",
                        null
                    ]
                },
                "_id": 0
            }
        }
    ]
});

MongoDB $switch

MongoDB $switch
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "price": 1,
                "priceSummary": {
                    "$switch": {
                        "branches": [
                            {
                                "case": {
                                    "$eq": [
                                        "$price",
                                        50000
                                    ]
                                },
                                "then": "equals"
                            },
                            {
                                "case": {
                                    "$gt": [
                                        "$price",
                                        50000
                                    ]
                                },
                                "then": "greater than"
                            },
                            {
                                "case": {
                                    "$lt": [
                                        "$price",
                                        50000
                                    ]
                                },
                                "then": "less than"
                            }
                        ]
                    }
                }
            }
        }
    ]
});

MongoDB $concat

MongoDB $concat
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$project": {
                "id": 1,
                "name_Description": {
                    "$concat": [
                        "$name",
                        "_",
                        "$description"
                    ]
                }
            }
        }
    ]
});

MongoDB $max

MongoDB $max
let aggregate = await g.sys.db.aggregate({
    instance: "mongodb",
    database: "inventory",
    collection: "customers",
    aggregateQuery:[
        {
            "$group": {
                "_id": "$price",
                "maxQuantity": {
                    "$max": "$quantity"
                }
            }
        }
    ]
});

Structure

Aggregate method
let aggregate = await g.sys.db.aggregate({
    instance: "INSTANCE_NAME",
    database: "DATABASE_NAME",
    collection: "COLLECTION_NAME",
    aggregateQuery: [
        {
            "$match":
                {
                    "COLUMN_NAME": "VALUE"
                }
        }
    ],
    headers: {
        "x-am-response-case": "capitalCase", // noChange | camelCase | capitalCase | constantCase | dotCase | headerCase | noCase | paramCase | pascalCase | pathCase | sentenceCase | snakeCase
        "x-am-response-object-type": "make_flat", // no_action | make_flat
        "x-am-meta": "true", // true, false
        "x-am-secret": "PROVIDE_SECRET_ID",
        "x-am-internationalization": "USER_I18N_ID",
        "x-am-run-in-sandbox": "2",
        "x-am-content-type-response": "text/xml", // application/json | text/xml | text/yaml | text/plain | text/html | application/octet-stream
        "x-am-cache-control": "reset_cache", // no_action | reset_cache
        "x-am-get-encrypted-data": "get_only_encryption", // no_encryption | get_only_encryption | get_data_and_encryption
        "x-am-sandbox-timeout": "13000",
        "x-no-compression": "true", // true | false
        "x-am-encrypted-payload": "true", // user will send 'true', when payload is encrypted for transfer
        // Authorization headers
        "x-am-authorization": "AUTHORIZATION_TOKEN",
        "x-am-user-authorization": "API_USER_TOKEN",
        "x-aws-authorization": "AWS_TOKEN",
        "x-google-authorization": "GOOGLE_TOKEN",
        "x-azure-authorization": "AZURE_TOKEN",
    },
});