Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8884655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:06:48+00:00 2026-06-14T21:06:48+00:00

Assuming I have a collection called posts (in reality it is a more complex

  • 0

Assuming I have a collection called “posts” (in reality it is a more complex collection, posts is too simple) with the following structure:

> db.posts.find()

{ "_id" : ObjectId("50ad8d451d41c8fc58000003"), "title" : "Lorem ipsum", "author" : 
"John Doe", "content" : "This is the content", "tags" : [ "SOME", "RANDOM", "TAGS" ] }

I expect this collection to span hundreds of thousands, perhaps millions, that I need to query for posts by tags and group the results by tag and display the results paginated. This is where the aggregation framework comes in. I plan to use the aggregate() method to query the collection:

db.posts.aggregate([
  { "$unwind" : "$tags" },
  { "$group" : {
      _id: { tag: "$tags" },
      count: { $sum: 1 }
  } }
]);

The catch is that to create the paginator I would need to know the length of the output array. I know that to do that you can do:

db.posts.aggregate([
  { "$unwind" : "$tags" },
  { "$group" : {
      _id: { tag: "$tags" },
      count: { $sum: 1 }
  } }
  { "$group" : {
      _id: null,
      total: { $sum: 1 }
  } }
]);

But that would discard the output from previous pipeline (the first group). Is there a way that the two operations be combined while preserving each pipeline’s output? I know that the output of the whole aggregate operation can be cast to an array in some language and have the contents counted but there may be a possibility that the pipeline output may exceed the 16Mb limit. Also, performing the same query just to obtain the count seems like a waste.

So is obtaining the document result and count at the same time possible? Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:06:49+00:00Added an answer on June 14, 2026 at 9:06 pm
    1. Use $project to save tag and count into tmp
    2. Use $push or addToSet to store tmp into your data list.

    Code:

    db.test.aggregate(
        {$unwind: '$tags'}, 
        {$group:{_id: '$tags', count:{$sum:1}}},
        {$project:{tmp:{tag:'$_id', count:'$count'}}}, 
        {$group:{_id:null, total:{$sum:1}, data:{$addToSet:'$tmp'}}}
    )
    

    Output:

    {
        "result" : [
                {
                        "_id" : null,
                        "total" : 5,
                        "data" : [
                                {
                                        "tag" : "SOME",
                                        "count" : 1
                                },
                                {
                                        "tag" : "RANDOM",
                                        "count" : 2
                                },
                                {
                                        "tag" : "TAGS1",
                                        "count" : 1
                                },
                                {
                                        "tag" : "TAGS",
                                        "count" : 1
                                },
                                {
                                        "tag" : "SOME1",
                                        "count" : 1
                                }
                          ]
                  }
          ],
          "ok" : 1
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a simple structure that looks like this: public class Range {
I need to implement a prioritised collection. Assuming we have the following three values
assuming I have the following array: views = [ { :user_id => 1, :viewed_at
Assuming I have a column called A and I want to check if A
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
Assuming I have a comments model and a posts model, What code can I
Is the following test method thread-safe, assuming that this is being called from multiple
I have a collection I'm unable to drop, I'm assuming that the - in
I have the following method being called in a Java EE web application. public
Using Java, assuming v1.6. I have a collection where the unique index is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.