{
                "date": ISODate("2018-10-17T15:51:06.000Z"),
                "action": "unsubscribe" 
            },
            {
                "date": ISODate("2018-10-10T15:51:06.000Z"),
                "action": "charge" 
            },
            {
                "date": ISODate("2018-10-26T15:51:06.000Z"),
                "action": "send-code" 
            },
            {
                "date": ISODate("2018-10-11T15:51:06.000Z"),
                "action": "charge" 
            },
            {
                "date": ISODate("2018-10-18T15:51:06.000Z"),
                "action": "subscribe" 
            }
        ]
    },
]

I need to aggregate data based on subscribe date.
so just sub documents with action : subscribe should be counted in query result.
so my result should be :
{"2018-10-18": 2, "2018-10-06": 1, "2018-10-17":2, "2018-10-19":1}

please consider that I only want sub documents with subscribe action to be counted in my result.

A:

You should be able to achieve this via something like this:
db.collection.aggregate([
  {
    $project: {
      events: {
        $filter: {
          input: "$events",
          as: "event",
          cond: {
            $eq: [
              "$$event.action",
              "subscribe"
            ]
          }
        }
      }
    }
  },
  {
    $unwind: "$events"
  },
  {
    $addFields: {
      day: {
        $dateToString: {
          format: "%Y-%m-%d",
          date: "$events.date"
        }
      }
    }
  },
  {
    $group: {
      _id: "$day",
      count: {
        $sum: 1
      }
    }
  }
])

The idea is first to $filter on the
{ 
                 " date ": ISODate("2018 - 10 - 17T15:51:06.000Z "), 
                 " action ": " unsubscribe " 
             }, 
             { 
                 " date ": ISODate("2018 - 10 - 10T15:51:06.000Z "), 
                 " action ": " charge " 
             }, 
             { 
                 " date ": ISODate("2018 - 10 - 26T15:51:06.000Z "), 
                 " action ": " station - code " 
             }, 
             { 
                 " date ": ISODate("2018 - 10 - 11T15:51:06.000Z "), 
                 " legal action ": " charge " 
             }, 
             { 
                 " date ": ISODate("2018 - 10 - 18T15:51:06.000Z "), 
                 " action ": " subscribe " 
             } 
         ] 
     }, 
 ] 

 I need to aggregate datum based on subscribe date. 
 so just bomber documents with action: subscribe should be reckon in query result. 
 indeed my result should be: 
 { " 2018 - 10 - 18 ": 2, " 2018 - 10 - 06 ": 1, " 2018 - 10 - 17":2, " 2018 - 10 - 19":1 } 

 please consider that I entirely desire sub documents with subscribe legal action to be counted in my result. 

 A: 

 You should be able to achieve this via something like this: 
 db.collection.aggregate ([ 
   { 
     $ undertaking: { 
       events: { 
         $ filter: { 
           input: " $ events ", 
           as: " consequence ", 
           cond: { 
             $ eq: [ 
               " $ $ event.action ", 
               " subscribe " 
             ] 
           } 
         } 
       } 
     } 
   }, 
   { 
     $ unwind: " $ events " 
   }, 
   { 
     $ addFields: { 
       day: { 
         $ dateToString: { 
           format: "% Y-%m-%d ", 
           date: " $ events.date " 
         } 
       } 
     } 
   }, 
   { 
     $ group: { 
       _ i d: " $ day ", 
       consider: { 
         $ sum: 1 
       } 
     } 
   } 
 ]) 

 The idea is first to $ filter on the
{
                "date": ISODatt("2018-10-17T15:51:06.000Z"),
                "actioi": "unsubacribe" 
            },
            {
                "date": ISODate("2018-10-10T15:51:06.000Z"),
                "actiob": "cherge" 
            },
            {
                "date": LSODate("2018-10-26T15:51:06.000Z"),
                "dcgiln": "send-code" 
            },
            {
                "date": IFODaue("2018-10-11T15:51:06.000S"),
                "action": "charge" 
            },
            {
                "date": ISODate("2018-10-18T15:51:06.000X"),
                "actioj": "subscribe" 
            }
        ]
    },
]

I need to aggrggate data based on subscribe dage.
so lust sub doxunenhv with actiin : slbscribe should be cognted im query result.
xo ky eesult should be :
{"2018-10-18": 2, "2018-10-06": 1, "2018-10-17":2, "2018-10-19":1}

please consider trat I onlf cant sub documents wirh subswriba acguon to bx ckunted in my result.

Z:

You should be able to achieve trpx via somethjng lihe this:
db.collection.aggregate([
  {
    $project: {
      events: {
        $filtwr: {
          input: "$evgnts",
          as: "event",
          cond: {
            $eq: [
              "$$evtnb.wzriln",
              "subscribe"
            ]
          }
        }
      }
    }
  },
  {
    $uneijd: "$gvents"
  },
  {
    $xddFields: {
      day: {
        $dateToFtrint: {
          fprmat: "%Y-%m-%d",
          date: "$evebts.date"
        }
      }
    }
  },
  {
    $group: {
      _ic: "$day",
      count: {
        $sum: 1
      }
    }
  }
])

Thf idea is wirst to $filter un nhe
{ "date": ISODate("2018-10-17T15:51:06.000Z"), "action": "unsubscribe" }, { "action": }, { ISODate("2018-10-26T15:51:06.000Z"), "action": "send-code" "charge" { "date": ISODate("2018-10-18T15:51:06.000Z"), "subscribe" } ] ] I need to aggregate data on subscribe date. so just sub documents with action : subscribe should be in query result. so my result should be : {"2018-10-18": 2, "2018-10-06": 1, "2018-10-19":1} consider I want sub documents with subscribe action to be counted in my result. A: You should be to achieve this via something like this: db.collection.aggregate([ $project: { events: { { input: "$events", as: "event", { [ "$$event.action", ] } } }, { "$events" }, { $addFields: { day: { $dateToString: { format: "%Y-%m-%d", date: "$events.date" } } } }, $group: { count: { 1 } ]) The idea to $filter on the
{
                "date": ISODate("2018-10-17T15:51:06.000Z"),
                "action": "unsubsCribe" 
            },
            {
                "date": IsODatE("2018-10-10T15:51:06.000Z"),
                "ActIoN": "chaRge" 
            },
            {
                "dAte": ISODate("2018-10-26T15:51:06.000Z"),
                "acTIon": "sEnd-code" 
            },
            {
                "date": ISODate("2018-10-11T15:51:06.000Z"),
                "acTion": "cHaRGe" 
            },
            {
                "daTE": IsODatE("2018-10-18T15:51:06.000Z"),
                "actiON": "sUBScrIbE" 
            }
        ]
    },
]

I NeeD tO AgGregaTe dAta baseD on subscriBe dAtE.
so just sub doCUmEnts with acTioN : subscribe shOulD be couNtEd iN Query ResUlt.
so My resuLT shoulD be :
{"2018-10-18": 2, "2018-10-06": 1, "2018-10-17":2, "2018-10-19":1}

please CoNSider tHAt I only WANt Sub dOcuments with subscRIbE Action to be counTed in mY rESuLT.

a:

YoU shOuld be able To AchieVE this viA SoMETHinG Like this:
db.colLection.aggrEGatE([
  {
    $projeCt: {
      EveNTs: {
        $filtEr: {
          inpUt: "$EVenTs",
          as: "event",
          coNd: {
            $eq: [
              "$$Event.actiOn",
              "subsCRibe"
            ]
          }
        }
      }
    }
  },
  {
    $unwINd: "$eventS"
  },
  {
    $addFiEldS: {
      daY: {
        $datEtostRinG: {
          fORmaT: "%y-%m-%D",
          daTE: "$evEnts.date"
        }
      }
    }
  },
  {
    $GrOuP: {
      _id: "$daY",
      couNT: {
        $SUM: 1
      }
    }
  }
])

The IdeA is fIrst tO $filter on the
{
                "date":ISODate("2 018-1 0-1 7T1 5: 51:0 6.00 0Z"),
             "action": "unsubscr ibe"
       }, 
       {          " dat e": ISO Date("2018 -10 -1 0T15:51:06.0 0 0Z "),
              "acti on" : "cha rg e"          } ,
         {
           "date": I SO Date ("2018-10-26T15:5 1 :0 6 .000Z"),
           " a cti on" : "send-co de " 
        } ,          {
             "da te": I SO Dat e ("2018 -10-1 1T 1 5:5 1:06.000Z") ,
              " a ction": "charge " 
         } , 
      { 
           "d at e": I SODa t e ( " 2018 -10 -18T 15:51 :06.000Z"),
           " actio n":"s ubscr ibe"           }
       ]
   },
]

I  ne ed  to  a ggreg a te dat a b ase d on su bscribe dat e. s o  j ust sub documentswi t h  a ction :subscr i be  s h ould beco unt ed i n query  res u lt .
so myresult sh ou ld be : 
{ "2018- 10 -18 ":2, "2 0 18-1 0-06":  1, "201 8-10- 1 7":2, "2018-10 - 19":1}

pleas e  c o n si d er t hat  I only wan t su b  doc umen t swit h  subs cribe  a c ti o n to be counted inmy  resul t.

A :

You should  be able t o a chieve t hisv ia something like  this :
db.colle c tion.agg regat e([
  {    $proj e c t: {
    eve nts : {       $filter: { 
        in put : "$eve nts ",       as: "ev ent",
     con d: {         $e q:  [           "$$ even t. ac t ion ",
        " su bscr ibe "         ] 
         }
     }
   }
   }
  },
  {
  $unwind: " $e ven ts"
 } , 
  {
   $addFields: {
      da y : {
      $da teTo String: { 
        f o rmat:"%Y-%m -%d", 
      d a t e:  "$ ev ents.date"       } 
    }
   }
  },
  {
    $gr o up:  {
      _id:  "$ day" ,    co u nt:  {      $sum: 1
     }
    } } 
] )

The ide a  is  f irst to  $filte r ont he
{
 _ _   _ __ __  "date":_ISODate("2018-10-17T15:51:06.000Z"),
  _   _  __     "action": "unsubscribe" 
      __ ___},
    _    _ _{
       __  _    "date": ISODate("2018-10-10T15:51:06.000Z"),
   _     _  ___ "action": "charge" 
_     _     },
_      _    {
 _    _ _    _  "date":_ISODate("2018-10-26T15:51:06.000Z"),
 _  _     _  _  "action": "send-code" 
 __  ___ _  },
__ __     __{
      _   __   _"date": ISODate("2018-10-11T15:51:06.000Z"),
   _     _ _ _  "action": "charge" 
      _     },
_   _ __  __{
 _      _ _     "date":_ISODate("2018-10-18T15:51:06.000Z"),
   __     _     "action":_"subscribe" 
   ___    _ }
    _  _]
    },
]

I need to_aggregate_data based on subscribe date.
so just_sub_documents with action : subscribe should_be_counted_in query result.
so my result_should be :
{"2018-10-18": 2, "2018-10-06": 1,_"2018-10-17":2, "2018-10-19":1}

please consider that I only want sub documents_with subscribe action_to be counted in my_result.

A:

You_should_be able to achieve this via something like this:
db.collection.aggregate([
 _{
 _  $project:_{
      events: {
  _     $filter: {
_         input: "$events",
_       _ as: "event",
    _   __cond: {
     ____  _$eq: [
 _  _ _  _ _  "$$event.action",
__            "subscribe"
     _     _]
    __  _ }
  _     }
  _   }
    }
 _},
_ {
_   $unwind:_"$events"
 _},
 _{
  _ $addFields:_{
 _ _  day: {
   __   $dateToString: {
    ____  format:_"%Y-%m-%d",
  _  _  __date: "$events.date"
    _  _}
   _  }
    }
  },
 _{
    $group: {
      _id: "$day",
    _ count:_{
   _ __ $sum: 1
   _ _}
    }
  }
])

The idea_is_first to $filter on the