'MongoDB Aggregation: How to get both group _id and total records count?

I'm working with a MongoDB collection that has a lot of duplicate keys. I regularly do aggregation queries to find out what those duplicates are, so that I can dig in and find out what is and isn't different about them.

Unfortunately the database is huge and duplicates are often intentional. What I'd like to do is to find the count of keys that have duplicates, instead of printing a result with thousands of lines of output. Is this possible?

(Side Note: I do all of my querying through the shell, so solutions that don't require external tools or a lot of code would be preferred, but I understand that's not always possible.)

Example Records:

[
                ObjectId("622f2d94ecf6a5076c2e230b"),
                ObjectId("622f329c6f10fe0490252611"),
                ObjectId("623026366f10fe0490254341"),
                ObjectId("623026de6f10fe0490254583"),
                ObjectId("6234346adec0b842dcceb790"),
                ObjectId("623434a86f10fe0490260db6"),
                ObjectId("62382f91dab1e245d4e152f4"),
                ObjectId("6238303b6f10fe0490265acf"),
                ObjectId("623bf2af700224301c756394"),
                ObjectId("623bf2f76f10fe04902729a4"),
                ObjectId("623c5a1f282a052c3c0bbdfd"),
                ObjectId("624bf013383df47699e6b141")
]

Here is the query that I've been using to find duplicates based on key:

db.getCollection('weldtestings').aggregate([
{
    $match: {
        weldId: {
            $in: [
                ObjectId("622f2d94ecf6a5076c2e230b"),
                ObjectId("622f329c6f10fe0490252611"),
                ObjectId("623026366f10fe0490254341"),
                ObjectId("623026de6f10fe0490254583"),
                ObjectId("6234346adec0b842dcceb790"),
                ObjectId("623434a86f10fe0490260db6"),
                ObjectId("62382f91dab1e245d4e152f4"),
                ObjectId("6238303b6f10fe0490265acf"),
                ObjectId("623bf2af700224301c756394"),
                ObjectId("623bf2f76f10fe04902729a4"),
                ObjectId("623c5a1f282a052c3c0bbdfd"),
                ObjectId("624bf013383df47699e6b141")]
        }
    }
},
{
    $facet: {
        "NDEfailedDate": [
                        {
                            $match: { testResult: 'Failed' }
                        },
                        {
                            $group: { 
                                _id: { $dateToString: { format: "%Y-%m-%d", date: "$testDate" } },
                                count: { $sum : 1 }
                            }
                        },
                        { $sort: { _id: 1 } }
                    ],
                    "NDEfailedCount": [
                        {
                            $match: { testResult: 'Failed' }
                        },
                        {
                            $group: {
                                _id: "$weldId",
                                data: { "$addToSet": "$testDate" }
                            }
                        },
                        { $count: "totalCount" }
                    ],
    }
}

])

Which gives me an output of:

{
"NDEfailedDate" : [ 
    {
        "_id" : "2022-04-08",
        "count" : 6.0
    }
],
"NDEfailedCount" : [ 
    {
        "totalCount" : 5
    }
]
}

The result I want to get instead:

"_id" : "2022-04-08",
"count" : 5


Solution 1:[1]

db.collection.aggregate([
  {
    $project: {
      _id: {
        $first: "$NDEfailedDate._id"
      },
      count: {
        $first: "$NDEfailedCount.totalCount"
      }
    }
  }
])

mongoplayground

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1