Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MongoDb Aggregate Total Count Before Grouping

I have an aggregation pipeline that groups objects and holds count for some specific field for grouped objects. You can reproduce the problem here: https://mongoplayground.net/p/2DGaiQDYDBP .

The schema is like this;

[
  {
    "_id": {
      "$oid": "63ce93ffb6e06322db59fdc0"
    },
    "fruit": "apple",
    "source": "tree",
    "is_fruit_important": "true"
  },
  {
    "_id": {
      "$oid": "63ce93ffb6e06322db59fdc1"
    },
    "fruit": "orange",
    "source": "tree",
    "is_fruit_important": "false"
  },
]

and the current query groups fruits by the source, and holds the count of important fruits for every group. After applying aggregation I get something like this after query:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "count": {
      "number_of_important_fruits": 1
    },
    "objects": [
      {
        "fruit": "apple",
        "id": "63ce93ffb6e06322db59fdc0",
        "is_fruit_important": "true",
        "source": "tree"
      },
      {
        "fruit": "orange",
        "id": "63ce93ffb6e06322db59fdc1",
        "is_fruit_important": "false",
        "source": "tree"
      }
    ],
    "source": {
      "source-of": "tree"
    }
  }
]

Is there a way to put the number of all fruits in the database to the response object. For example like this:

  {
    "total-count": 2,
    "result": [
      {
        "count": {
          "number_of_important_fruits": 1
        },
        "objects": [
          {
            "fruit": "apple",
            "id": "63ce93ffb6e06322db59fdc0",
            "is_fruit_important": "true",
            "source": "tree"
          },
          {
            "fruit": "orange",
            "id": "63ce93ffb6e06322db59fdc1",
            "is_fruit_important": "false",
            "source": "tree"
          }
        ],
        "source": {
          "source-of": "tree"
        }
      }
    ]
  }

They can be handled in separate aggregation pipelines but that’s what I would not like to implement. Any help would be highly appreciated.

>Solution :

Add one additional group stage just before the final $project, using $sum with $size for a total count, or add up the important counts for a total important count.

  {$group: {
      _id: null,
      result: {$push: "$$ROOT"},
      "count_total": {$sum: {$size: "$objects"}},
      "count_important": {$sum: "$count.number_of_important_fruits"}
  }},

Playground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading