c# - select from union select with order by and group by in mongodb using aggregation -


i try rewrite next sql query mongodb using c# aggregation framework, can`t understand how it. need union results.

select top 100 res.agent, res.type, res.opens ((select ua.clientdomain agent, ua.type type, count(*) opens                                                      treadconfirm rc inner join tuseragent ua on rc.useragentid = ua.id                                                     rc.userid = 2654 , rc.campaignid = 27442  , ua.type = 1                                                     group ua.clientdomain, ua.type)                                                 union                                                 (select ua.family agent, ua.type type, count(*) opens                                                      treadconfirm rc inner join tuseragent ua on rc.useragentid = ua.id                                                     rc.userid = 2654 , rc.campaignid = 27442 , ua.type <> 1                                                     group ua.family, ua.type)) res                                                 order res.opens desc 

it`s start code, not need

db.analytics.aggregate(     {         $match: { userid: 4749, campaignid: 93178}     },     {         $group :         {             _id :              {                  "family" : "$useragent.family",                 "type" : "$useragent.type",                 "clientdomain" : "$useragent.clientdomain",             }  ,             "opens":              {                  $sum : 1              }          }      },      {$sort :{"opens":-1}} ) 

i found answer, work code example. operator "$cond" helps me.

db.analytics.aggregate(      {          $match : {              "userid" : 4790,              "campaignid" : 93178}       },      {         $group :         {             _id :              {                  "type" : "$useragent.type",                 "clientdomain" : { $cond: { if: { $eq: [ "$useragent.type", 1 ] }, then: "$useragent.clientdomain", else: "$useragent.family" }},             }  ,             "opens":              {                  $sum : 1              }          }      },      {          $sort :{"opens":-1}      },      {          $limit:10      } ) 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -