Cómo agregar conteo dentro de la agregación de MongoDB. En esta agregación, estoy ordenando los datos por "categoría" de campo anidado profundo. Producto de catálogo es un ObjectId de producto de catálogo. Dentro del producto del catálogo tengo un ObjectId de categoría.
products = await StorageModel.aggregate([ { $lookup: { // Replace the Catelog Product ID with the Catelog Product from: "catalogs", localField: "catalogProduct", foreignField: "_id", as: "catalogs" } }, { $lookup: { // Replace the Category ID with the Category from: "categories", localField: "catalogs.category", foreignField: "_id", as: "catalogs.category" } }, { $addFields: { // Replace the Category with its name "catalogs.category": "$catalogs.category" } }, { $match: { "catalogs.category._id": { $in: ids } } }, { $lookup: { // Replace the Catelog Product ID with the Catelog Product from: "catalogs", localField: "catalogProduct", foreignField: "_id", as: "catalogProductNew" } }, { $count: "count" }, { $sort: { _id: -1 } }, { $skip: (page - 1) * 8 }, { $limit: 8 }, ]);
Devoluciones. Pero otro dato de la agregación fue borrado.
[ { count: 3 } ]