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