0

I have Signal record collection below:

Signal records:

[
  {
    "id": "5dc250e0d972e71c3b3e6e88",
    "signalStrength": -180,
    "signalbars": "3",
    "employee": "5db59227f0204855654075ee",
    "store": "5dc25092d972e71c3b3e6e87",
    "carrierName": "LT&T"
  },
  {
    "id": "5dc251f723760a24de167f8e",
    "signalStrength": -180,
    "signalbars": "3",
    "employee": "5db59227f0204855654075ee",
    "store": "5dc25092d972e71c3b3e6e87",
    "carrierName": "Sprint"
  },
  {
    "id": "5dc289affd1ea02f0fceb9ac",
    "signalStrength": -80,
    "signalbars": "3",
    "employee": "5db59227f0204855654075ee",
    "store": "5dc2899bfd1ea02f0fceb9ab",
    "carrierName": "LT&T"
  }
]

Basically i need to calculate carrier name based average values and total number of employees who has signal records for that carrier name

And i need to get following results

[
      {
        "_id": "LT&T",
        "averageSignal": -130,
        "strongestSignal": -80,
        "weakestSignal": -180,
        "averageBars": 3,
        "totalTests": 2,
        "totalTesters": 1
      },
      {
        "_id": "Sprint",
        "averageSignal": -180,
        "strongestSignal": -180,
        "weakestSignal": -180,
        "averageBars": 3,
        "totalTests": 1,
        "totalTesters": 1
      }
]

My code looks below for aggregation:

[
  {
    $match: query
  },
  {
    $group:
    {
      _id: "$carrierName",
      averageSignal: { $avg: "$signalStrength" },
      strongestSignal: { $max: "$signalStrength" },
      weakestSignal: { $min: "$signalStrength" },
      averageBars: { $avg: "$signalbars" },
      totalTests: { $sum: 1 },
    }
  },
  { $sort: { carrierName: -1 } },
]

am stuck how find total testers on above query

Any help would be great

Prajwal
  • 319
  • 4
  • 10
  • This question is not as same as the others @ Ashh. Other question answers all other criteria but not "totalTesters". but it does grouping on one criteria. I have tried grouping based on carrier name, But lost to find no. of testers in the same – Prajwal Nov 11 '19 at 06:52
  • 1
    Seems that you need to add this field to your group stage - "employee": {"$addToSet": "$employee"}, then follow it up with - { "$addFields": { "totalTesters": { "$size": "$employee" } } } – Mahesh Nayak Nov 11 '19 at 07:08
  • @Mahesh Nayak Thanks. Worked for me :) – Prajwal Nov 11 '19 at 07:14

0 Answers0