3

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
}]

1 Answer 1

8

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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.