Home > Software design >  How can I exclude results that contain a specific element from grouped results?
How can I exclude results that contain a specific element from grouped results?

Time:10-28

A: It should be output how many _ids are included by date grouped by date.

B: The number of elements in details in A. 
If it has element, count 1. not 0. If the document is as follows, the value counted after excluding from A becomes B

{
  _id: ObjectId
  details: array  //no elements
  createdAt: Date
}

C: The count of B becomes C, except when there are specific details.slaesManagerIds among B. 
details.salesManagerIds is provided as an array. 
For examples, 
[ObjecttId("612f57184205db63a3396a9e"), ObjectId("612cb021278f621a222087d7")]

I made query as follows.

https://mongoplayground.net/p/6sBxAmO_31y

It goes well until B. How can I write a query to get C ?

If you write and execute a query that can obtain C through the link above, you should get the following result.

[
  {
    "A": 2,
    "B": 1,
    "C": 1,
    "_id": "2018-05-19"
  },
  {
    "A": 3,
    "B": 3,
    "C": 1,
    "_id": "2018-05-18"
  }
]

CodePudding user response:

use $filter

db.collection.aggregate([
  {
    $group: {
      _id: {
        $dateToString: {
          format: "%Y-%m-%d",
          date: "$createdAt"
        }
      },
      A: {
        $sum: 1
      },
      B: {
        $sum: {
          $cond: [
            {
              $and: [
                {
                  $isArray: "$details"
                },
                {
                  $gt: [
                    {
                      $size: "$details"
                    },
                    0
                  ]
                }
              ]
            },
            1,
            0
          ]
        }
      },
      C: {
        $sum: {
          $cond: [
            {
              $and: [
                {
                  $isArray: "$details"
                },
                {
                  $gt: [
                    {
                      $size: "$details"
                    },
                    0
                  ]
                },
                {
                  $gt: [
                    {
                      $size: {
                        $filter: {
                          input: "$details",
                          as: "d",
                          cond: {
                            $and: [
                              {
                                $not: [
                                  {
                                    $in: [
                                      "$$d.salesManagerId",
                                      [
                                        ObjectId("612f57184205db63a3396a9e"),
                                        ObjectId("612cb021278f621a222087d7")
                                      ]
                                    ]
                                  }
                                ]
                              }
                            ]
                          }
                        }
                      }
                    },
                    0
                  ]
                }
              ]
            },
            1,
            0
          ]
        }
      }
    }
  },
  {
    $sort: {
      _id: -1
    }
  }
])

mongoplayground

  • Related