monodb之$cond条件及其if-else使用

报表生成中使用了mongodb的$cond 及其if else语句;mongodb不支持case when语句;
可参考mongodb样例链接:https://docs.mongodb.com/manual/reference/operator/aggregation/cond/#example
功能:对影片观看时间统计用户数(1、十分钟以内观看记录用户;2、10-30分钟;3、30-80分钟;4、80分钟以上);
temp_result = self.db[Constants.action_table].aggregate(
    [
        {
           "$match":{'actionInfo.status':7}
        },

        {"$group": {"_id": {

            "mac": '$mac',
            "sn": '$sn',
            "day_time": {"$substrBytes": ["$time", 0, 10]}
        },
            "duration": {'$sum': "$actionInfo.consumeTime"}
        }}
        ,
        { "$project":
                {
                    "_id": 0,
                    "day_time": "$_id.day_time",
                     "mac":"$_id.mac",
                     "sn": "$_id.sn",
                     "duration":"$duration",
                    "discount": {
                          "$cond": {
                              "if": { "$lt": ['$duration', 600000]},"then": "10分钟内",
                              "else": {
                                      "$cond": {
                                         "if": {  "$and": [{ "$gte": ["$duration",600000 ]}, { "$lt": ["$duration", 18000000]}]},"then": "10-30分钟",
                                         "else": {
                                            "$cond": {
                                                "if": {  "$and": [{ "$gte": ["$duration", 18000000]}, { "$lte": ["$duration", 48000000]}]},"then" : "30-80分钟",
                                                "else": "80分钟以上"
                                            }
                                          }
                                      }
                              }
                          }
                    }
                }},
        {"$out": "TotalUserAverageDailyLengthRound_temp"}]);

self.db["TotalUserAverageDailyLengthRound_temp"].aggregate([
     {"$group": {"_id": {
                                                "day_time": '$day_time',
                                                "discount": '$discount',
                                            }, "countUser": {'$sum': 1}}},
     {"$project":
            {
                                                        "_id": 0,
                                                        "day_time": "$_id.day_time",
                                                        "discount": "$_id.discount",
                                                        "countUser": "$countUser"}},
     {"$out": self.currentTable()}]);
return temp_result;

 

你可能感兴趣的:(mongdb,mongodb)