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

303
Visualizações
Getting count of total documents when using aggregation along with skip and limit

How can I get the total count of documents when using the aggregation along with limit and skip like in the following query?

  db.Vote.aggregate({
       $match: {
           tid: "e6d38e1ecd",
           "comment.topic": {$exists: 1},
       }
   },{
       $group: {
           _id: {
               topic: "$comment.topic",
               text_sentiment: "$comment.text_sentiment"
               
           },
           total: {$sum: 1},
           
       }
   },{
       $project: {
           topic: {
               name: "$_id.topic",
               occurence: "$total"
           },
           sentiment: "$_id.text_sentiment"
       }
   },{
       $sort: {"topic.occurence": -1}
   })
    .skip(SKIP)
    .limit(LIMIT)

Here I would always get the documents bounded by LIMIT, but at each query, I also want to know the total documents. How can I do that?

EDIT While some of you have suggested using $facet, I am not sure how to use that. For example, the aggregation returns 50 documents without limit. How can I use $facet such that it not only returns LIMIT documents but also the meta-data that contains the total documents.

Here is a working query from MongoDB Playground that I created. How can I use $facet or any other method to get the total count.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

One option is using $facet, but the disadvantage is that it will group all your documents to one document:

db.Vote.aggregate(
  {$match: {tid: "e6d38e1ecd",  "comment.topic": {$exists: 1}}},
  {$group: {_id: {topic: "$comment.topic", text_sentiment: "$comment.text_sentiment"},
           total: {$sum: 1}}
   },
  {$project: {topic: {name: "$_id.topic",  occurence: "$total"},
           sentiment: "$_id.text_sentiment"}
  },
  {$facet: {
    data: [{$sort: {"topic.occurence": -1}}, {$skip: SKIP}, {$limit: LIMIT}],
    total: [{ $count: "total" }]
  }}
)

Other option is to use $setWindowFields to add the total count to each document, which allows to keep them as separate documents:

db.Vote.aggregate(
  {$match: {tid: "e6d38e1ecd",  "comment.topic": {$exists: 1}}},
  {$group: {_id: {topic: "$comment.topic", text_sentiment: "$comment.text_sentiment"},
           total: {$sum: 1}}
   },
  {$project: {topic: {name: "$_id.topic",  occurence: "$total"},
           sentiment: "$_id.text_sentiment"}
  },
  {
    $setWindowFields: {output: {totalCount: {$count: {}}}}
  })
    .skip(SKIP)
    .limit(LIMIT)
about 4 years ago · Juan Pablo Isaza Relatório
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