首页 > 代码库 > mongodb比较复杂的综合统计计算,性能还不差

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"}
     }
 }
])


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