mongodb比较复杂的综合统计计算,性能还不差

db.app_users.aggregate([
 {
     $project:{
         fs:1,
         ls:1,
         subt:{$subtract:[‘$ls‘,‘$fs‘]},
         days714:{
             $cond: [ { $gte:[‘$ls‘,(Math.round((new Date()).getTime() / 1000)-14*24*60*60)]},1,0]
         },
         days1430:{
             $cond: [
             {
                 $and:[
                    { $lte:[‘$ls‘,(Math.round((new Date()).getTime() / 1000)-14*24*60*60)]},
                    { $gte:[‘$ls‘,(Math.round((new Date()).getTime() / 1000)-30*24*60*60)]}
                 ]
             },1,0]
         },
                
         days30:{
             $cond: [ { $lte:[‘$ls‘,(Math.round((new Date()).getTime() / 1000)-30*24*60*60)]},1,0]
         },
         
         days815:{
             $cond: [
             {
                 $and:[
                    { $lte:[‘$ls‘,(Math.round((new Date()).getTime() / 1000)-8*24*60*60)]},
                    { $gte:[‘$ls‘,(Math.round((new Date()).getTime() / 1000)-15*24*60*60)]}
                 ]
             },1,0]
         },
         days1531:{
             $cond: [
             {
                 $and:[
                    { $lte:[‘$ls‘,(Math.round((new Date()).getTime() / 1000)-15*24*60*60)]},
                    { $gte:[‘$ls‘,(Math.round((new Date()).getTime() / 1000)-31*24*60*60)]}
                 ]
             },1,0]
         },
                
         days31:{
             $cond: [ { $lte:[‘$ls‘,(Math.round((new Date()).getTime() / 1000)-31*24*60*60)]},1,0]
         },
     }
 },
 {
     $match:{
         subt:{$lt:24*60*60},
         ls:{ $lt:(Math.round((new Date()).getTime() / 1000)-7*24*60*60)}
     }
 },
 {
     $group:{
         ‘_id‘:null,
         days7_14: {$sum: "$days714"},
         days14_30: {$sum: "$days1430"},
         days_more30: {$sum: "$days30"},
         days8_15: {$sum: "$days715"},
         days15_31: {$sum: "$days1531"},
         days_more31: {$sum: "$days30"}
     }
 }
])


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。