Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从MongoDB中的时间戳表中获取每天的登录数?_Mongodb - Fatal编程技术网

如何从MongoDB中的时间戳表中获取每天的登录数?

如何从MongoDB中的时间戳表中获取每天的登录数?,mongodb,Mongodb,我在MongoDB中有一个表,看起来像这样: 用户ID |事件类型|时间戳 每次用户登录时,都会在此表中添加一个新条目。我想从给定的一天和那一天检索一些事件,所以我想得到 db.collection.aggregate([ { $match: {TIMESTAMP: timestamp} }, { $group: { _id: "$TIMESTAMP", events: {$sum: 1}

我在MongoDB中有一个表,看起来像这样:

用户ID |事件类型|时间戳

每次用户登录时,都会在此表中添加一个新条目。我想从给定的一天和那一天检索一些事件,所以我想得到

db.collection.aggregate([
    {
        $match: {TIMESTAMP: timestamp}
    },
    {
        $group: {
            _id: "$TIMESTAMP",
            events: {$sum: 1}
        }
    }
])
{
    _id: userid,
    events: [
        {
            type: "",
            timestamp: timestamp
        }
    ]
}
2016年1月1日-5项活动

等等

如何实现这一点?

您可以使用

差不多

db.collection.aggregate([
    {
        $match: {TIMESTAMP: timestamp}
    },
    {
        $group: {
            _id: "$TIMESTAMP",
            events: {$sum: 1}
        }
    }
])
{
    _id: userid,
    events: [
        {
            type: "",
            timestamp: timestamp
        }
    ]
}
  • 匹配表示它必须匹配时间戳
  • 该组表示“按时间戳分组,并将1添加到名为events的新字段”
  • 只需将小写时间戳更改为所需的时间戳

    或者如果您只需要一个时间戳

    db.testCollection.find({TIMESTAMP: timestamp})
    
    --

    或者您可以将您的模式更改为

    db.collection.aggregate([
        {
            $match: {TIMESTAMP: timestamp}
        },
        {
            $group: {
                _id: "$TIMESTAMP",
                events: {$sum: 1}
            }
        }
    ])
    
    {
        _id: userid,
        events: [
            {
                type: "",
                timestamp: timestamp
            }
        ]
    }
    
    这将使用mongodb的选项来创建数组,而不仅仅是普通值

    编辑


    您必须在时间戳介于两者之间的位置进行查询。否则,这将无法按预期工作

    我如何通过ODM原则使用它?我想获取给定日期的所有事件,而不是时间戳。还有,如何将其传递给mongo shell?当我粘贴你的代码时,什么也没发生。对不起,我对条令一无所知。如果你想在条令中得到答案,你应该编辑你的问题。如果您只想计数,可以执行以下操作:第二个时间戳与前一个时间戳相同,但添加了24小时。