Get distinct values from an aggregation pipeline

Hello,

I need your help !
In the same aggreation pipeline, I want to retrieve a list of wines and the list of region associated :
The exepected output is :
wineList : {
“_id”: 47,
“product_name”: “Les menuts”,
“domaine”: “Chateau Grace Dieu”,
“vintage”: 2015,
“color”: “Rouge”,
“region”: “Bordeaux”,
“price”: null,
“quantity”: 1,
“stock”: 1,
“wines”: [
{
“id”: “652008ea98d388c0f1a75f7d”,
“wine_id”: 63,
“quantity”: 1,
“cellar_location”: “LF”
}
]
},
{
“_id”: 45,
“product_name”: “La cupa”,
“domaine”: “Chateau Laroque”,
“vintage”: 2020,
“color”: “Rouge”,
“region”: “Languedoc-Roussillon”,
“price”: null,
“quantity”: 0,
“stock”: 0,
“wines”: [
{
“id”: “652008ea98d388c0f1a75fb5”,
“wine_id”: 61,
“cellar_location”: “LF”
}
]
},
{
“_id”: 49,
“product_name”: “La Moniale”,
“domaine”: “domaine le portail”,
“vintage”: 2021,
“color”: “Rouge”,
“region”: “Loire”,
“price”: null,
“quantity”: 0,
“stock”: 0,
“wines”: [
{
“id”: “652008ea98d388c0f1a75fa7”,
“wine_id”: 65,
“quantity”: 0,
“cellar_location”: “LF”
}
]
}
},

regionList : {
“Languedoc-Roussillon”,
“Bordeaux”,
“Loire”
}

I use this query to generate the wine List :

WineModel.aggregate(
[{ $match:filter},
{
$group: {
_id: ‘$main_reference’,
product_name: {
$first: “$product_name”,
},
domaine: {
$first: “$domaine”,
},
vintage: {
$first: “$vintage”,
},
color: {
$first: “$color”,
},
region: {
$first: “$region”,
},
price: {
$first: “$price”,
},
quantity: {
$sum: “$quantity”,
},
stock: {
$first: {
$cmp: [{$sum: “$quantity”},0]
}
},
wines: {
$push: {
id: ‘$_id’,
wine_id: ‘$wine_id’,
quantity: ‘$quantity’,
cellar_location: ‘$cellar_location’
}
},
}
}
],
{ maxTimeMS: 60000, allowDiskUse: true })

Thanks a lot for your help !

Please read Formatting code and log snippets in posts and update your post as it is quite unreadable at the moment, and also please share MongoDB Playground link with sample documents and the query you have used.