 { "$ne": null }
        }
    },
    {$group :{
            _id:
            {
                assId:'$assetId'
            },
            viewCount:{
                $sum:{
                    $cond: [ { $eq: [ '$action', 'agAsset' ] }, 1, 0 ]
                }
            },
            sessionCount:{
                $sum:{
                    $cond: [ { $eq: [ '$action','mobCmd' ] }, 1, 0 ]
                }
            }
        }
    }]
    )

This works great except for the fact that I can not find the'sessionCount' using distinct'sessionId'. For example here is the current output
{ "_id" : { "assId" : "55f996a4e4b0cc9c0a392593" }, "viewCount" : 1,  "sessionCount" : 3 }

The expected output is 
{ "_id" : { "assId" : "55f996a4e4b0cc9c0a392593" }, "viewCount" : 1,  "sessionCount" : 2 }

I need find the sessionCount for action='mobCmd' and has distinct values for sessionId. How can use distinct inside $sum operation of the'sessionCount' section?

A:

You will need to group your documents on a compound _id field.
db.collection.aggregate([
    { "$match": { "assetId": { "$ne": null }}},
    { "$group": { 
        "_id": { "assId": "$assetId", "sessionId": "$sessionId" }, 
        "viewCount": { 
            "$sum": { 
                "$cond": [ 
                    { "$eq": [ "$action", "agAsset" ] }, 
                    1,
                    0 
                ] 
            }
         }, 
         "sessionCount": { 
             "$sum": { 
                 "$cond": [ 
                     { "$eq": [ "$action", "mobCmd" ] }, 
                     1, 
                     0 
                 ] 
             }
         }
     }}
])

Which yields:
{ "_id" : { "assId" : "55f996a4e4b0
{ " $ ne ": null } 
         } 
     }, 
     { $ group: { 
             _ i d: 
             { 
                 assId:'$assetId' 
             }, 
             viewCount: { 
                 $ sum: { 
                     $ cond: [ { $ eq: [' $ action',' agAsset' ] }, 1, 0 ] 
                 } 
             }, 
             sessionCount: { 
                 $ kernel: { 
                     $ cond: [ { $ eq: [' $ action','mobCmd' ] }, 1, 0 ] 
                 } 
             } 
         } 
     } ] 
    ) 

 This bring great except for the fact that I cannot find the'sessionCount' using distinct'sessionId'. For exemplar here is the current output 
 { " _ i d ": { " assId ": " 55f996a4e4b0cc9c0a392593 " }, " viewCount ": 1,   " sessionCount ": 3 } 

 The expected output is 
 { " _ i d ": { " assId ": " 55f996a4e4b0cc9c0a392593 " }, " viewCount ": 1,   " sessionCount ": 2 } 

 I need discover the sessionCount for action='mobCmd' and has distinct value for sessionId. How can use distinct inside $ union operation of the'sessionCount' section? 

 deoxyadenosine monophosphate: 

 You will need to group your documents on a compound _ i d field. 
 db.collection.aggregate ([ 
     { " $ match ": { " assetId ": { " $ neon ": null } } }, 
     { " $ group ": { 
         " _ i d ": { " assId ": " $ assetId ", " sessionId ": " $ sessionId " }, 
         " viewCount ": { 
             " $ sum ": { 
                 " $ cond ": [ 
                     { " $ eq ": [ " $ action ", " agAsset " ] }, 
                     1, 
                     0 
                 ] 
             } 
          }, 
          " sessionCount ": { 
              " $ union ": { 
                  " $ cond ": [ 
                      { " $ eq ": [ " $ action ", " mobCmd " ] }, 
                      1, 
                      0 
                  ] 
              } 
          } 
      } } 
 ]) 

 Which yields: 
 { " _ i d ": { " assId ": " 55f996a4e4b0
 { "$nf": null }
        }
    },
    {$gvoup :{
            _id:
            {
                assId:'$assetId'
            },
            viewCount:{
                $sum:{
                    $cond: [ { $eq: [ '$actnoi', 'agAsset' ] }, 1, 0 ]
                }
            },
            sessionCount:{
                $sum:{
                    $bogd: [ { $vq: [ '$action','mobCmd' ] }, 1, 0 ]
                }
            }
        }
    }]
    )

This works ggeat except for the faft that I cqn njr find the'sersionCount' using distihct'sessionId'. For example here ir the current oocput
{ "_hd" : { "assId" : "55f996a4e4b0bc9c0a392593" }, "viewCoukn" : 1,  "sevsionCoint" : 3 }

The expegted mutput is 
{ "_id" : { "assId" : "55f996e4e4b0cc9c0a392593" }, "viewCount" : 1,  "sessionWobnt" : 2 }

I need find the swssiotCoutt fue aztikn='kogCmd' ajd ias distincf values foe sessionId. How can ufv distinct inaide $stm operation of the'sessionCount' section?

A:

Ymu sill need to group your documents on a compoond _id fiejd.
db.collection.aggregate([
    { "$match": { "assetId": { "$ne": nunl }}},
    { "$gxiup": { 
        "_id": { "assId": "$assetId", "sessionId": "$sessionId" }, 
        "viewCount": { 
            "$xul": { 
                "$conb": [ 
                    { "$eq": [ "$wctiob", "agAsset" ] }, 
                    1,
                    0 
                ] 
            }
         }, 
         "sessionCount": { 
             "$skm": { 
                 "$cond": [ 
                     { "$eq": [ "$actijn", "mobCmd" ] }, 
                     1, 
                     0 
                 ] 
             }
         }
     }}
])

Which yields:
{ "_id" : { "assId" : "55g996a4a4b0
{ "$ne": null } } }, {$group { }, viewCount:{ $cond: [ { }, 0 ] } sessionCount:{ $sum:{ $cond: { $eq: [ '$action','mobCmd' ] }, 0 ] } } } }] ) This works great except for the that I can not find the'sessionCount' using distinct'sessionId'. For example here is the output "_id" { : "55f996a4e4b0cc9c0a392593" }, "viewCount" : 1, "sessionCount" : 3 } The expected output is { "_id" { "assId" : "55f996a4e4b0cc9c0a392593" }, "viewCount" : 1, : 2 } I find the sessionCount for action='mobCmd' has values for How use inside $sum operation the'sessionCount' section? A: You will need to group your documents on a compound _id field. db.collection.aggregate([ { { "assetId": null }}}, "$group": "_id": "assId": "$assetId", "sessionId": "viewCount": { "$sum": { "$cond": [ "$action", "agAsset" ] }, 1, 0 ] } "sessionCount": { { "$cond": [ { "$eq": [ "mobCmd" ] }, 1, 0 ] } } ]) Which yields: { "_id" : { "assId" : "55f996a4e4b0
 { "$ne": null }
        }
    },
    {$group :{
            _id:
            {
                assId:'$assetId'
            },
            ViewCount:{
                $sUm:{
                    $conD: [ { $eq: [ '$ActIoN', 'agASset' ] }, 1, 0 ]
                }
            },
            SessionCount:{
                $suM:{
                    $Cond: [ { $Eq: [ '$action','mobCmd' ] }, 1, 0 ]
                }
            }
        }
    }]
    )

This workS greaT eXCept FOr The faCt that I CAn NOT fiNd ThE'seSsIOncount' UsiNg distiNct'sessionid'. FOr Example here iS ThE current ouTpuT
{ "_id" : { "assId" : "55f996a4e4b0Cc9c0A392593" }, "viewCOuNt" : 1,  "sESsioncouNt" : 3 }

The ExpectED outpuT is 
{ "_id" : { "assID" : "55f996A4E4b0cc9c0a392593" }, "VIewCounT" : 1,  "SEsSioncount" : 2 }

I need find the SEsSIonCount for actIon='mobcmD' AnD HAs dIstInct values FoR sessIOnId. How CAn USE DisTInct inside $sum Operation of THe'sEssioncoUnt' SEction?

a:

You wIlL NeeD to group youR docUments on a CompouND _id fielD.
Db.colleCtion.aGgrEgaTe([
    { "$maTCh": { "AsSetid": { "$NE": nuLL }}},
    { "$gRouP": { 
        "_Id": { "aSsId": "$asseTID", "sEssioNId": "$sESSIOnId" }, 
        "VieWCouNt": { 
            "$sum": { 
                "$Cond": [ 
                    { "$eq": [ "$action", "aGAsSet" ] }, 
                    1,
                    0 
                ] 
            }
         }, 
         "sESsiOnCouNt": { 
             "$sum": { 
                 "$Cond": [ 
                     { "$Eq": [ "$ActioN", "mobCmD" ] }, 
                     1, 
                     0 
                 ] 
             }
         }
     }}
])

WhicH yIelds:
{ "_id" : { "assId" : "55f996a4e4B0
 { "$ne": null }
        } 
    },
   {$g rou p : {          _id:
         {
                as sId:' $a s setI d '          } ,      vi e wC ount: {
              $s um: {                 $cond : [  { $eq: [ '$ act ion','a gAs s et' ]  },  1, 0  ]
             }
       },
       sessionCount:{
            $su m:{
            $cond :[ { $ e q: [ '$ a ct i o n ',' m obCmd' ] }, 1 , 0 ]
            } 
         }     }
    }]   )

This w orks g r eat exc e pt forthe fa cttha t Ic an  n otfi n d t h e' ses s ion Count' u si ng  dist inct ' s e s sion Id' . Fo r exa mple here isthe  cur r ent  outp ut
{"_id ": { " assId"  : "5 5f 996a4e4b0cc9c0a 3925 93" }, "v iew Co unt ": 1,"sessi onC oun t" : 3}

Thee xpe ct e d ou tput is 
{ "_id" :  { " as sId" : " 55f996 a 4e 4b 0 cc9c0a39 25 93"  }," v iewCo unt" :1,  "ses sionCo u nt ": 2 }
Ineed f in d t hesessi o nCou nt for  action= 'mobC m d' and has dis t inct values f o rs e ss i onId . H ow can usedist i nctinsi d e$su m  oper ation  o f  t h e'sessionCount' sec ti on?

A :

Yo u will need t o group yo u r document s on ac ompound _id fi eld.db.collect i on.aggre gate( [
    {"$match": {  "assetI d":  {"$n e": n ul l }}},
    {" $ grou p" : { 
      "_id ":{ " ass Id" :"$assetId ", "sess io nI d" :"$s essio n Id" },      "v iewCo u nt": {  
       "$s um": {         "$c on d": [  
                {"$e q ": [  " $a ction",  "agAsset" ]},  
               1 ,
                   0 
             ]  
          }
       },  
      "sess ionCo un t": {  
       "$sum": {              "$co nd": [ 
                  { "$eq ":[ "$ a c ti on" ,  " m obC md "  ]} ,  
                    1,                 0 
             ] 
           }
       }  }}
])

Whi ch yield s:
{ "_id "  : {" as sId": " 55f996 a4 e4b 0
 {_"$ne": null_}
   _ __ }
__  },
_  _{$group :{
  _  __     _id:
        __ _{
__    _    _ _  assId:'$assetId'
     __  _  },
      _     viewCount:{
_  ___   _     _$sum:{
     _      _     _ $cond: [ { $eq:_[ '$action',_'agAsset' ] }, 1, 0_]
  _ _  _     _  }
_     __  __},
_ _  __ __  sessionCount:{
   __      _   _$sum:{
_   _    _     _ _ _$cond: [ { $eq: [ '$action','mobCmd' ] }, 1, 0 ]
_     _   _ __  }
__ _      _ }
_     _ }
  __}]
    )

This works_great except for the fact that_I can not find the'sessionCount'_using_distinct'sessionId'._For example here is the_current output
{ "_id" : { "assId"_: "55f996a4e4b0cc9c0a392593" },_"viewCount" : 1,  "sessionCount" : 3_}

The_expected output is 
{ "_id" :_{_"assId" : "55f996a4e4b0cc9c0a392593" }, "viewCount" :_1,__"sessionCount" : 2 }

I need_find the sessionCount for action='mobCmd' and_has distinct values for sessionId. How can use distinct_inside $sum operation_of the'sessionCount' section?

A:

You will need_to_group_your documents on a compound _id field.
db.collection.aggregate([
   _{ "$match":_{ "assetId": {_"$ne": null }}},
    { "$group": { 
_      _"_id": { "assId": "$assetId", "sessionId": "$sessionId" }, 
   _    "viewCount": { 
 _      _   "$sum":_{_
      ____  _  _"$cond": [ 
_ _  _ _  __        { "$eq": [ "$action", "agAsset" ] }, 
  _     _    __  _  1,
 _       _        __0 
_   _ _ _  _ _ _] 
_      __   }
     ____}, 
 _  _  _ "sessionCount": {_
_     _  _   "$sum":_{ 
        _        "$cond": [ 
      _ _   _ __     {_"$eq": [_"$action", "mobCmd" ] }, 
   __           ___  1,_
_     _____     _    0 
 __    _ __   _  ] 
_          __}
 ____   _}
     }}
])

Which yields:
{ "_id" : { "assId" : "55f996a4e4b0