Vous pouvez utiliser $project &$unwind &$group de agrégation
framework pour obtenir un résultat plus proche de vos besoins.
> db.countries.aggregate({$project:{a:'$data.country.neighbor.name'}},
{$unwind:'$a'},
{$unwind:'$a'},
{$group:{_id:'a',res:{$addToSet:'$a'}}})
{
"result" : [
{
"_id" : "a",
"res" : [
"Colombia",
"Malaysia",
"Switzerland",
"Costa Rica",
"Austria"
]
}
],
"ok" : 1
}
$unwind utilisé deux fois car le tableau de noms est profondément imbriqué. Et cela ne fonctionnera que si le neighbor L'attribut est un tableau. Dans votre exemple, un champ voisin (Malaisie) n'est pas un tableau