mongo-cmd

  • $each
{$addToSet:{orders:{$each: part_orders}}

  • $reduce
{$addFields: {days:{$reduce: {
      input: '$days',  
      initialValue: [],  
      in: {$setUnion: ['$$value','$$this']}
}}}},

db.monthly_2019_6.aggregate([
    // {$match: {userId:{$eq: 1000708130}}},
    {$project: {userId:1,org_id:1,days:'$days.day',all_durs:1,total_stars:1,pt:{$sum:['$pt','$delta_pt']}}},
    {$group: {_id: '$userId',
        userid: {$first:'$userId'},
        orgId:{$first: '$org_id'},
        days:{$addToSet: '$days'},
        durs:{$sum: '$all_durs'},
        star:{$sum:'$total_stars'},
        pt:{$sum: '$pt'}
    }},
    {$unwind: '$days'},
    {$unwind: '$days'},
    {$group: {_id: '$userid',
        userid: {$first:'$userid'},
        orgId:{$first: '$orgId'},
        days:{$addToSet:'$days'},
        durs:{$first: '$durs'},
        star:{$first:'$star'},
        pt:{$first: '$pt'}
    }},
    {$addFields: {attendDay: {$size: '$days'}}},
    {$group: {_id:'$orgId',
        orgId: {$first: '$orgId'},
        attendUser: {$sum: 1},
        userAttendDayList: {$push: '$attendDay'},
        attendDay: {$sum: '$attendDay'},
        userDurationList: {$push: '$durs'},
        duration: {$sum: '$durs'},
        star: {$sum: '$star'},
        pt: {$sum: '$pt'}
    }}    
])

  • $group and $cond
$group : ['_id':'$org_id',
    c_e:[$sum:[ $cond: [ [ $eq: [ '$from_mys', 'true' ] ], 1, 0 ]]],
    c_f:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_type', 0 ] ] ] ], 1, 0 ]]],
    c_g:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_type', 1 ] ] ] ], 1, 0 ]]],
    c_h:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_type', 2 ] ] ] ], 1, 0 ]]],
    c_i:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_type', 3 ] ] ] ], 1, 0 ]]],
    c_j:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_status', 1 ] ] ] ], 1, 0 ]]],
    c_k:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_status', 2 ] ] ] ], 1, 0 ]]],
    c_l:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_status', 3 ] ] ] ], 1, 0 ]]],
    c_m:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_status', 4 ] ] ] ], 1, 0 ]]],
    c_n:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_status', 5 ] ] ] ], 1, 0 ]]],
    c_o:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$card_status', 0 ] ] ] ], 1, 0 ]]],
    c_p:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 1 ] ] ] ], 1, 0 ]]],
    c_q:[ $addToSet: [ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 1 ] ] ] ], '$user_id', null ]] ],
    c_r:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 2 ] ] ] ], 1, 0 ]]],
    c_s:[ $addToSet: [ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 2 ] ] ] ], '$user_id', null ]] ],
    c_t:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 3 ] ] ] ], 1, 0 ]]],
    c_u:[ $addToSet: [ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 3 ] ] ] ], '$user_id', null ]] ],
    c_v:[$sum:[ $cond: [ [$and: [ [ $ne: [ '$card_type', 4 ] ],[ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 6 ] ] ] ], 1, 0 ]]],
    c_w:[ $addToSet: [ $cond: [ [$and: [ [ $ne: [ '$card_type', 4 ] ],[ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 6 ] ] ] ], '$user_id', null ]] ],
    c_x:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 12 ] ] ] ], 1, 0 ]]],
    c_y:[ $addToSet: [ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $eq: [ '$as_month_card', 12 ] ] ] ], '$user_id', null ]] ],
    c_z:[$sum:[ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $not: [ [ $in: [ '$as_month_card', [ 1, 2, 3 ,6,12] ] ] ] ] ] ], 1, 0 ]]],
    c_aa:[ $addToSet: [ $cond: [ [$and: [ [ $eq: [ '$from_mys', 'false' ] ], [ $not: [ [ $in: [ '$as_month_card', [ 1, 2, 3 ,6,12] ] ] ] ] ] ], '$user_id', null ]] ]
]

你可能感兴趣的:(mongo-cmd)