Mongodb区间查询

Mongodb区间查询,mongodb,Mongodb,我有一个mongodb数据库,其中有一个集合用户。这些文档如下所示: { "_id" : ObjectId("5e0431821d0f986bdb338e82"), "location" : { "uuid" : "b8e671f3-742e-4a24-ae3e-6cd543e7c5bb", "name" : "London" }, "time" : ISODate("2019-12-16T16:37:45.000Z"),

我有一个mongodb数据库,其中有一个集合用户。这些文档如下所示:

{
    "_id" : ObjectId("5e0431821d0f986bdb338e82"),
    "location" : {
        "uuid" : "b8e671f3-742e-4a24-ae3e-6cd543e7c5bb",
        "name" : "London"
    },
    "time" : ISODate("2019-12-16T16:37:45.000Z"),
    "actor" : {
        "user" : {
            "uuid" : "c42f7cbf-84a6-4912-b414-6afc873e229d",
            "name" : "User 1"
        }
    }
},
{
    "_id" : ObjectId("5e0431d31d0f986bdb338e83"),
    "location" : {
        "uuid" : "b8e671f3-742e-4a24-ae3e-6cd543e7c5bb",
        "name" : "London"
    },
    "time" : ISODate("2019-12-16T17:00:00.000Z"),
    "actor" : {
        "user" : {
            "uuid" : "c42f7cbf-84a6-4912-b414-6afc873e229d",
            "name" : "User 1"
        }
    }
},
{
    "_id" : ObjectId("5e0431e41d0f986bdb338e84"),
    "location" : {
        "uuid" : "b8e671f3-742e-4a24-ae3e-6cd543e7c5bb",
        "name" : "London"
    },
    "time" : ISODate("2019-12-16T17:05:00.000Z"),
    "actor" : {
        "user" : {
            "uuid" : "c42f7cbf-84a6-4912-b414-6afc873e229d",
            "name" : "User 1"
        }
    }
},
{
    "_id" : ObjectId("5e04320f1d0f986bdb338e85"),
    "location" : {
        "uuid" : "b8e671f3-742e-4a24-ae3e-6cd543e7c5bb",
        "name" : "London"
    },
    "time" : ISODate("2019-12-16T17:06:00.000Z"),
    "actor" : {
        "user" : {
            "uuid" : "d42f7cbf-84a6-4912-b414-6afc873e229d",
            "name" : "User 2"
        }
    }
},
{
    "_id" : ObjectId("5e0432191d0f986bdb338e86"),
    "location" : {
        "uuid" : "b8e671f3-742e-4a24-ae3e-6cd543e7c5bb",
        "name" : "London"
    },
    "time" : ISODate("2019-12-16T17:15:00.000Z"),
    "actor" : {
        "user" : {
            "uuid" : "d42f7cbf-84a6-4912-b414-6afc873e229d",
            "name" : "User 2"
        }
    }
}
根据上述文件

  • 用户1-16:37:45第一次刷卡
  • 用户1-在17:00:00进行第二次卡片扫描
  • 用户1-17:05:00第三次刷卡
  • 用户2-17:06:00第一次刷卡
  • 用户2-17:15:00第二次刷卡 Mongodb组查询
当我执行上面的MongoDB查询时,会得到下面的结果

[{
    "start_time" : 16.0,
    "end_time" : 17.0,
    "total_user_arrivals" : 1
},
{
    "start_time" : 17.0,
    "end_time" : 18.0,
    "total_user_arrivals" : 2
}]
扩展结果

  • 用户1在16:00:00到17:00:00之间进行首次卡片扫描
  • 用户2在17:00:00到18:00:00之间进行首次卡片扫描

在分组之前,您需要获取用户的第一个事务

db.getCollection('test').aggregate([
{
    $match: {
        time: { $gt: new Date('2019-12-16T00:00:00.000Z'), $lte: new Date('2019-12-17T00:00:00.000Z') },
        'location.uuid': 'b8e671f3-742e-4a24-ae3e-6cd543e7c5bb',
    },
},
{ $sort: { time: 1 } },
{
    $group: {
        _id: {
            uuid: '$actor.user.uuid',
        },
        tran: { $first: '$$ROOT' },
    },
},
{
    $group: {
        _id: {
            hour: { $hour: { date: '$tran.time' } },
            minute: {
                $subtract: [
                    { $minute: { date: '$tran.time'} },
                    { $mod: [{ $minute: '$tran.time' }, 60] },
                ],
            },
        },
        count: { $addToSet: '$tran.actor.user.uuid' },
    },
},
{
    $project: {
        start_time: { $add: ['$_id.hour', 0] },
        end_time: { $add: ['$_id.hour', 1] },
        total_user_arrivals: { $size: '$count' },
        _id: 0,
    },
},
])
请于
[{
    "start_time" : 16.0,
    "end_time" : 17.0,
    "total_user_arrivals" : 1
},
{
    "start_time" : 17.0,
    "end_time" : 18.0,
    "total_user_arrivals" : 1
}]
db.getCollection('test').aggregate([
{
    $match: {
        time: { $gt: new Date('2019-12-16T00:00:00.000Z'), $lte: new Date('2019-12-17T00:00:00.000Z') },
        'location.uuid': 'b8e671f3-742e-4a24-ae3e-6cd543e7c5bb',
    },
},
{ $sort: { time: 1 } },
{
    $group: {
        _id: {
            uuid: '$actor.user.uuid',
        },
        tran: { $first: '$$ROOT' },
    },
},
{
    $group: {
        _id: {
            hour: { $hour: { date: '$tran.time' } },
            minute: {
                $subtract: [
                    { $minute: { date: '$tran.time'} },
                    { $mod: [{ $minute: '$tran.time' }, 60] },
                ],
            },
        },
        count: { $addToSet: '$tran.actor.user.uuid' },
    },
},
{
    $project: {
        start_time: { $add: ['$_id.hour', 0] },
        end_time: { $add: ['$_id.hour', 1] },
        total_user_arrivals: { $size: '$count' },
        _id: 0,
    },
},
])