0

I want count of all documents before doing grouping as show in my Query. The purpose of this if ai get count of all documents then I can calculate Percentage(%) of each individual students count. I tried with $count but after using that I'm not able to group my query. Is there any way 1st I can have counts of all documents after using $match function then store it to another variable without losing data.

This is the Query

const data = await AttendanceSchema.aggregate([
      { $match: { subjectID: mongoose.Types.ObjectId(`${req.params.Sid}`) } },

      // Before Group I also want count of all documents

      { $group: { _id: "$studentID", count: { $sum: 1 } } },
      {
        $lookup: {
          from: "students",
          localField: "_id",
          foreignField: "_id",
          as: "student",
        },
      },
      {
        $project: {
          "student.createdAt": 0,
          "student.updatedAt": 0,
          "student.__v": 0,
          "student.password": 0,
        },
      },
    ]);

The data I'm getting from this query

{

    "data": [
        {
            "_id": "635d40803352895afffdc294",
            "count": 3,
            "student": [
                {
                    "_id": "635d40803352895afffdc294",
                    "name": "D R",
                    "email": "d@gmail.com",
                    "number": "9198998888",
                    "rollNumber": 202,
                    "departmentID": "635a8ca21444a47d65d32c1a",
                    "classID": "635a92141a081229013255b4",
                    "position": "Student"
                }
            ]
        },
        {
            "_id": "635eb8898dea5f437789b751",
            "count": 4,
            "student": [
                {
                    "_id": "635eb8898dea5f437789b751",
                    "name": "V R",
                    "email": "v@gmail.com",
                    "number": "9198998899",
                    "rollNumber": 203,
                    "departmentID": "635a8ca21444a47d65d32c1a",
                    "classID": "635a92141a081229013255b4",
                    "position": "Student"
                }
            ]
        }
    ]
}

The desired output:


    "data": [
        {
            "_id": "635d40803352895afffdc294",
            "totalCount: 7, //This is what I want
            "count": 3, 
            "student": [
                {
                    "_id": "635d40803352895afffdc294",
                    "name": "D R",
                    "email": "d@gmail.com",
                    "number": "9198998888",
                    "rollNumber": 202,
                    "departmentID": "635a8ca21444a47d65d32c1a",
                    "classID": "635a92141a081229013255b4",
                    "position": "Student"
                }
            ]
        },
        {
            "_id": "635eb8898dea5f437789b751",
            "totalCount: 7, //This is what I want
            "count": 4,
            "student": [
                {
                    "_id": "635eb8898dea5f437789b751",
                    "name": "V R",
                    "email": "v@gmail.com",
                    "number": "9198998899",
                    "rollNumber": 203,
                    "departmentID": "635a8ca21444a47d65d32c1a",
                    "classID": "635a92141a081229013255b4",
                    "position": "Student"
                }
            ]
        }
    ]
}
Om Adde
  • 21
  • 2

1 Answers1

0

You could add a few more stages to you aggregation pipeline to output what you want.

Here are the extra stages:

  {
    "$group": {
      "_id": null,
      "totalCount": {"$sum": "$count"},
      "docs": {"$push": "$$ROOT"}
    }
  },
  {
    "$project": {
      "docs": {
        "$map": {
          "input": "$docs",
          "as": "doc",
          "in": {
            "$mergeObjects": [
              "$$doc",
              {"totalCount": "$totalCount"}
            ]
          }
        }
      }
    }
  },
  {"$unwind": "$docs"},
  {"$replaceWith": "$docs"}

Try it on mongoplayground.net.

rickhg12hs
  • 10,638
  • 6
  • 24
  • 42