novalagung
novalagung

Reputation: 11502

MongoDB aggregate - distinct count specific element

I have collection contain documents below. I want to use aggregate to count how many customer inside, but I got some issues. I can get the total row, but not the total (unique) customer.

[{
    _id: "n001",
    channel: "Kalipare",
    trans: {
        _id: "trans001",
        customerID: "customerCXLA93",
        customerName: "Kalipare Fried Chicked"
    }
}, {
    _id: "n002",
    channel: "Kalipare",
    trans: {
        _id: "trans002",
        customerID: "customerCXLA93",
        customerName: "Kalipare Fried Chicked"
    }
}, {
    _id: "n003",
    channel: "Kalipare",
    trans: {
        _id: "trans003",
        customerID: "customerPWR293",
        customerName: "Kalipare Papabun"
    }
}, {
    _id: "n004",
    channel: "Kalipare",
    trans: {
        _id: "trans004",
        customerID: "customerPWR293",
        customerName: "Kalipare Papabun"
    }
}, {
    _id: "n005",
    channel: "Tumpakrejo",
    trans: {
        _id: "trans005",
        customerID: "customerPWR293",
        customerName: "Tumpakrejo Big Burger"
    }
}]

This is my code.

db.col.aggregate([
    { $group: {
        _id: "$channel",
        totalRow: { $sum: 1 }
    } }
])

How should I do to count the unique customer and generate data like this.

[{
    _id: "Kalipare",
    totalRow: 4,
    totalCustomer: 2
}, {
    _id: "Tumpakrejo",
    totalRow: 1,
    totalCustomer: 1
}]

Upvotes: 3

Views: 6600

Answers (1)

chridam
chridam

Reputation: 103325

For getting the unique customer count requires creating a set of the unique customer ids within the group pipeline using the $addToSet operator. Once you get the array, use the $size operator within the $project pipeline to get the length, which would give you the unique count.

Running the following aggregation pipeline will give you the desired result:

db.col.aggregate([
    {
        "$group": {
            "_id": "$channel",
            "totalRows": { "$sum": 1 },
            "totalCustomer": { "$addToSet": "$trans.customerID" }
        }
    },
    {
        "$project": {
            "totalRows": 1,
            "totalCustomers": { "$size": "$totalCustomer" }
        }
    }
])

Output:

{
    "result" : [ 
        {
            "_id" : "Tumpakrejo",
            "totalRows" : 1,
            "totalCustomers" : 1
        }, 
        {
            "_id" : "Kalipare",
            "totalRows" : 4,
            "totalCustomers" : 2
        }
    ],
    "ok" : 1
}

Upvotes: 8

Related Questions