Raw query examples | HumongouS.io Documentation
Raw query examples
Writing MongoDB aggregation queries can sometimes be a daunting task. It can take a little bit of a time to get used to it, but in order to get you started quickly, we've compiled a list of examples of the most common analytic metrics, written in the MongoDB query language.
Tweak these examples to fit your needs and if there's something you try to achieve without success, don't hesisate to contact us at support@humongous.io. We always have a couple of MongoDB ninjas ready to help :)
All examples will be based on the data below.
total
delivered
address
created_at
23.5
false
221B Baker Street, London
6/15/2019
32.15
true
1007 Mountain Drive, Gotham
6/16/2019
26.2
true
5th floor, 90 Bedford Street
6/16/2019
18
false
1938 Sulivan Ln, Metropolis
6/17/2019
28.5
true
4 Privet Drive, Little Whinging, Surrey
6/17/2019
19.7
true
1313 Webfoot Walk, Duckburg, Calisota
6/17/2019

Count all orders

Query
db.orders.aggregate([{
    $group: { _id: null, value: { $sum: 1 } }
}])
Result
{ _id: null, value: 6 }
Was this helpful ?
Yes
No

Count all delivered orders

Query
db.orders.aggregate([
    { $match: { delivered: true } },
    { $group: { _id: null, value: { $sum: 1 } } }
])
Result
{ _id: null, value: 4 }
Was this helpful ?
Yes
No

Sum of all orders

Query
db.orders.aggregate([
    { $group: { _id: null, value: { $sum: "$total" } } }
])
Result
{ _id: null, value: 148.05 }
Was this helpful ?
Yes
No

Group by orders delivery status

Query
db.orders.aggregate([
    { $group: { _id: "$delivered", value: { $sum: 1 } } }
])
Result
{ _id: null, value: 2 }
{ _id: "delivered", value: 4 }
Was this helpful ?
Yes
No

Calculate ratio of all documents

Query
db.orders.aggregate([
    // Phase 1 : Count total of delivered and non delivered orders.
    {
        $group: {
        _id: "$delivered",
        delivered_total: { $sum: 1 }
        }
    },

    // Phase 2 : Count total of all orders.
    {
        $group: {
            _id: null,
            total: { $sum: "$delivered_total" },
            delivery_data: { $push: { total: "$delivered_total", delivered: "$_id" } }
        }
    },

    // Phase 3 : Return an array of all statuses along with $total.
    { $unwind: "$delivery_data" },

    // Phase 4 : Return ratio and label for each status.
    {
        $project: {
            _id: "$delivery_data.delivered",
            value: { $divide: ["$delivery_data.total", "$total"] }
        }
    }
])
Result
{ _id: null, value: 0.33 }
{ _id: "delivered", value: 0.67 }
Was this helpful ?
Yes
No

Calculate ratio of only delivered orders

Query
db.orders.aggregate([
    // Phase 1 : Count total of delivered and non delivered orders.
    {
        $group: {
        _id: "$delivered",
        delivered_total: { $sum: 1 }
        }
    },

    // Phase 2 : Count total of all orders.
    {
        $group: {
            _id: null,
            total: { $sum: "$delivered_total" },
            delivery_data: { $push: { total: "$delivered_total", delivered: "$_id" } }
        }
    },

    // Phase 3 : Return an array of all statuses along with $total.
    { $unwind: "$delivery_data" },

    // Phase 4 : Return ratio and label for each status.
    {
        $project: {
            _id: "$delivery_data.delivered",
            value: { $divide: ["$delivery_data.total", "$total"] }
        }
    },

    // Phase 5 : Only return ratio for delivered orders.
    {
        $match: { _id: true }
    }
])
Result
{ _id: null, value: 0.67 }
Was this helpful ?
Yes
No