У меня есть следующая коллекция:
{"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"}
{"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"}
{"orderID" : "30688", "branch" : "CO", "customerID" : "11396783", "customerEmail" : "foo@bar.com"}
{"orderID" : "89765", "branch" : "CO", "customerID" : "54157526", "customerEmail" : ""}
{"orderID" : "89765", "branch" : "CO", "customerID" : "54157526", "customerEmail" : ""}
{"orderID" : "21546", "branch" : "CO", "customerID" : "20103585", "customerEmail" : "xxx@yyy.com"}
{"orderID" : "21546", "branch" : "CO", "customerID" : "20103585", "customerEmail" : "xxx@yyy.com"}
{"orderID" : "21546", "branch" : "KA", "customerID" : "89374792", "customerEmail" : "aaa@ccc.com"}
{"orderID" : "21794", "branch" : "NY", "customerID" : "78125522", "customerEmail" : ""}
Мне нужно получить все уникальные идентификаторы customerID в определенной ветви, для которой customerEmail не равен NULL.То, что я ожидаю для "филиала": "CO"
{"customerID" : "11396783", "customerEmail" : "foo@bar.com"}
{"customerID" : "20103585", "customerEmail" : "xxx@yyy.com"}
Пока я пробовал:
db.collection.aggregate([
{ $match: { branch: "CO" } },
{ $group:
{
_id: { customer:"$customerID"}
}
},
{
$group: {_id:"$_id.customer"}
},
{
$addFields: { email: "$customerEmail"}
}
]);
, но это не приносит поле электронной почты.