Vous pouvez map() . Utilisez Array.map() avec mangouste car il renvoie un tableau, et vous feriez mieux d'utiliser simplement le $group _id que d'utiliser $push
const subCategory = (await SubCategory.aggregate([
{ '$match': { category: "dining" } },
{ '$group': { '_id': "$name" } }
])).map(({ _id }) => _id);
Ou avec Cursor.map() si vous utilisez la Collection sous-jacente depuis le pilote principal :
const subCategory = await SubCategory.collection.aggregate([
{ '$match': { category: "dining" } },
{ '$group': { '_id': "$name" } }
]).map(({ _id }) => _id).toArray();
La même chose avec find() si vous ne voulez pas les résultats "distincts":
const subCategory = (await Subcategory.find({ category: "dining" }))
.map(({ name }) => name);
Ou avec le Cursor.map()
const subCategory = await Subcategory.collection.find({ category: "dining" })
.map(({ name }) => name).toArray();
Vous pouvez également utiliser distinct() , qui fait essentiellement une variation du processus d'agrégation et de la map() "under the hood" (le "retourner uniquement la partie champ" et non la méthode d'agrégation distincte) :
const subCategory = await SubCategory.distinct("name",{ category: "dining" });
MongoDB lui-même ne renverra rien d'autre qu'un document BSON, et une simple chaîne n'est PAS un document BSON.