基于unix时间戳的Mongodb聚合

Ali*_*hdi 6 mongodb nosql

我用谷歌搜索了很多,但没有找到任何有用的解决方案...我想找到每日用户的总数.我有一个名为session_log的集合,其中包含以下文档

{
    "_id" : ObjectId("52c690955d3cdd831504ce30"),
    "SORTID" : NumberLong(1388744853),
    "PLAYERID" : 3,
    "LASTLOGIN" : NumberLong(1388744461),
    "ISLOGIN" : 1,
    "LOGOUT" : NumberLong(1388744853)
}
Run Code Online (Sandbox Code Playgroud)

我想从LASTLOGIN汇总...

这是我的查询:

db.session_log.aggregate(
    { $group : {
        _id: {
            LASTLOGIN : "$LASTLOGIN"
        },
        count: { $sum: 1 }
    }}
);
Run Code Online (Sandbox Code Playgroud)

但它是按每个登录时间聚合,而不是每天.任何帮助,将不胜感激

chr*_*dam 22

MongoDB 4.0及更新版本

使用 $toDate

db.session_log.aggregate([
    { "$group": {
        "_id": {
            "$dateToString": {
                "format": "%Y-%m-%d",
                "date": {
                    "$toDate": { 
                        "$multiply": [1000, "$LASTLOGIN"]
                    }
                }
            }
        },
        "count": { "$sum": 1 }
    } }
])
Run Code Online (Sandbox Code Playgroud)

要么 $convert

db.session_log.aggregate([
    { "$group": {
        "_id": {
            "$dateToString": {
                "format": "%Y-%m-%d",
                "date": {
                    "$convert": { 
                        "input":  { 
                            "$multiply": [1000, "$LASTLOGIN"] 
                        }, 
                        "to": "date"
                    }
                }
            }
        },
        "count": { "$sum": 1 }
    } }
])
Run Code Online (Sandbox Code Playgroud)

MongoDB> = 3.0和<4.0:

db.session_log.aggregate([
    { "$group": {
        "_id": {
            "$dateToString": {
                "format": "%Y-%m-%d",
                "date": {
                    "$add": [
                        new Date(0), 
                        { "$multiply": [1000, "$LASTLOGIN"] }
                    ]
                }
            }
        },
        "count": { "$sum": 1 }
    } }
])
Run Code Online (Sandbox Code Playgroud)

您需要LASTLOGIN通过将值乘以1000 将字段转换为毫秒时间戳

{ "$multiply": [1000, "$LASTLOGIN"] }
Run Code Online (Sandbox Code Playgroud)

,然后转换为日期

"$add": [
    new Date(0),
    { "$multiply": [1000, "$LASTLOGIN"] }
]
Run Code Online (Sandbox Code Playgroud)

这样就可以在完成$project管道通过添加毫秒的时间到零毫秒Date(0)的对象,然后解压$year,$month,$dayOfMonth从转换的日期部分,然后您可以在您使用$group的管道按天组的文件.

因此,您应该将聚合管道更改为:

var project = {
    "$project":{ 
        "_id": 0,
        "y": {
            "$year": {
                "$add": [
                    new Date(0),
                    { "$multiply": [1000, "$LASTLOGIN"] }
                ]
            }
        },
        "m": {
            "$month": {
                "$add": [
                    new Date(0),
                    { "$multiply": [1000, "$LASTLOGIN"] }
                ]
            }
        }, 
        "d": {
            "$dayOfMonth": {
                "$add": [
                    new Date(0),
                    { "$multiply": [1000, "$LASTLOGIN"] }
                ]
            }
        }
    } 
},
group = {   
    "$group": { 
        "_id": { 
            "year": "$y", 
            "month": "$m", 
            "day": "$d"
        },  
        "count" : { "$sum" : 1 }
    }
};
Run Code Online (Sandbox Code Playgroud)

运行聚合管道:

db.session_log.aggregate([ project, group ])
Run Code Online (Sandbox Code Playgroud)

会给出以下结果(基于样本文件):

{ "_id" : { "year" : 2014, "month" : 1, "day" : 3 }, "count" : 1 }
Run Code Online (Sandbox Code Playgroud)

改进将是在单个管道中运行上述内容

var group = {   
    "$group": { 
        "_id": {    
            "year": {
                "$year": {
                    "$add": [
                        new Date(0),
                        { "$multiply": [1000, "$LASTLOGIN"] }
                    ]
                }
            },
            "mmonth": {
                "$month": {
                    "$add": [
                        new Date(0),
                        { "$multiply": [1000, "$LASTLOGIN"] }
                    ]
                }
            }, 
            "day": {
                "$dayOfMonth": {
                    "$add": [
                        new Date(0),
                        { "$multiply": [1000, "$LASTLOGIN"] }
                    ]
                }
            }
        },  
        "count" : { "$sum" : 1 }
    }
};
Run Code Online (Sandbox Code Playgroud)

运行聚合管道:

db.session_log.aggregate([ group ])
Run Code Online (Sandbox Code Playgroud)