I have the following collection:
{"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "[email protected]"}
{"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "[email protected]"}
{"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "[email protected]"}
{"orderID" : "89765", "branch" : "CO", "customerID" : "54157526", "customerEmail" : ""}
{"orderID" : "89765", "branch" : "CO", "customerID" : "54157526", "customerEmail" : ""}
{"orderID" : "21546", "branch" : "CO", "customerID" : "20103585", "customerEmail" : "[email protected]"}
{"orderID" : "21546", "branch" : "CO", "customerID" : "20103585", "customerEmail" : "[email protected]"}
{"orderID" : "21546", "branch" : "KA", "customerID" : "89374792", "customerEmail" : "[email protected]"}
{"orderID" : "21794", "branch" : "NY", "customerID" : "78125522", "customerEmail" : ""}
I need to get all unique customerIDs in a certain branch whose customerEmail is not null. What I expect for "branch":"CO"
{"customerID" : "11396783", "customerEmail" : "[email protected]"}
{"customerID" : "20103585", "customerEmail" : "[email protected]"}
So far I've tried:
db.collection.aggregate([
{ $match: { branch: "CO" } },
{ $group:
{
_id: { customer:"$customerID"}
}
},
{
$group: {_id:"$_id.customer"}
},
{
$addFields: { email: "$customerEmail"}
}
]);
but it doesn't bring the email field.