MongoDB facet subpipeline filter with equal values

50 views Asked by At

I have a collection with document structure

type: POSSIBLE VALUES ARE a1 or a2
emp_name: <<String>>
status: POSSIBLE VALUES ARE 0 or 1

I am using mongodb aggregate facet with 2 subpipeline

first subpipeline: filter type with a1, then group on emp_name, and project as empName and count

a1: [
    {
      $match: {
        type: "a1",
      },
    },
    {
      $group: {
        _id: {
          emp_name: "$emp_name",
        },
        count: {
          $sum: 1,
        },
      },
    },
    {
      $project: {
        empName : "$_id.emp_name",
        count: 1,
        _id: 0,
      },
    },
  ]

second subpipeline: filter type with a2 and project as emp_name, status and type

a2: [
    {
      $match: {
        type: "a2",
      },
    },
    {
      $project: {
        type: 1,
        emp_name: 1,
        status: 1,
        _id: 0,
      },
    }
  ]

facet will return 2 array of JSONObject, one have empName and count and other will have emp_name, status and type.

is it possible to get count, empName, status and type under a single object by validating equality on first subpipeline empName and second subpipeline emp_name

Thanks in advance

0

There are 0 answers