Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
Add count inside MongoDB aggregation

How to add count inside MongoDB aggregation. In this aggregation I'm sorting data by deep nested field "category". Catalog product is an ObjectId of catalog product. Inside catalog product I have an ObjectId of category.

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 },
            ]);

Returns. But another data from aggregation was erased.

[ { count: 3 } ]

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda