MongoDB查询今天的呼叫列表

时间:2015-04-29 12:52:44

标签: mongodb mongoose

这是MongoDB中用户表中一个用户的数据

{
"_id" : ObjectId("553799187174b8c402151d06"),
"modified" : ISODate("2015-04-22T12:50:32.477Z"),
"name" : "Suresh",
"userId" : "sursha6398",
"created" : ISODate("2015-04-22T12:50:32.457Z"),
"deleted" : false,
"call_schedule" : [ 
    {
        "callType" : "a",
        "callDate" : ISODate("2015-01-14T18:30:00.000Z"),
        "_id" : ObjectId("553799187174b8c402151d0e")
    }, 
    {
        "callType" : "a",
        "callDate" : ISODate("2015-01-31T18:30:00.000Z"),
        "_id" : ObjectId("553799187174b8c402151d0d")
    }, 
    {
        "callType" : "b",
        "callDate" : ISODate("2015-02-19T18:30:00.000Z"),
        "_id" : ObjectId("553799187174b8c402151d0c")
    }, 
    {
        "callType" : "b",
        "callDate" : ISODate("2015-03-02T18:30:00.000Z"),
        "_id" : ObjectId("553799187174b8c402151d0b")
    }, 
    {
        "callType" : "c",
        "callDate" : ISODate("2015-03-17T18:30:00.000Z"),
        "_id" : ObjectId("553799187174b8c402151d0a")
    }, 
    {
        "callType" : "b",
        "callDate" : ISODate("2015-03-30T18:30:00.000Z"),
        "_id" : ObjectId("553799187174b8c402151d09")
    }, 
    {
        "callType" : "a",
        "callDate" : ISODate("2015-04-08T18:30:00.000Z"),
        "_id" : ObjectId("553799187174b8c402151d08")
    }, 
    {
        "callType" : "d",
        "callDate" : ISODate("2015-04-22T18:30:00.000Z"),
        "_id" : ObjectId("553799187174b8c402151d07")
    }
],
"__v" : 0

}

有数百个用户,每个用户都安排了一个或多个呼叫。 我需要使用mongodb查询找到今天使用相应用户ID调度的呼叫总数。

1 个答案:

答案 0 :(得分:2)

要获得使用相应userid计划的通话总数,您需要使用MongoDB的 aggregation framework 。聚合管道由$match管道阶段组成,作为第一步,过滤集合中的文档,该文档在今天开始和明天之前具有呼叫计划,即查询标准是日期范围。然后,下一个管道阶段使用$unwind运算符来解构call_schedule数组,以便您可以为数组中的每个元素获取文档的输出。每个输出文档都使用元素值替换数组。在$unwind操作之后,进一步过滤文档以仅获得符合$match条件的文档。下一个管道阶段将是$group运算符,然后您可以按用户标识对文档进行分组,并使用组中每个文档的$sum运算符计算汇总总和。因此,您的最终聚合管道将如下所示:

var start = new Date(); // today's date
var end = new Date(new Date().setDate(new Date().getDate()+1));

var pipeline = [
    {
        "$match": {
            "call_schedule.callDate": {
                "$gte": start,
                "$lt": end
            }
        }
    },
    {
        "$unwind": "$call_schedule"
    },
    {
        "$match": {
            "call_schedule.callDate": {
                "$gte": start,
                "$lt": end
            }
        }
    },
    {
        "$group": {
            "_id": {
                "userId": "$userId"
            },
            "total": {
                "$sum": 1
            }
        }
    }  
];

db.collection.aggregate(pipeline);

<强>输出

/* 0 */
{
    "result" : [ 
        {
            "_id" : {
                "userId" : "sursha6398"
            },
            "total" : 3 // <-- this gives you the total number of calls made by userid today
        }
    ],
    "ok" : 1
}

- 更新 -

要获取已过滤的呼叫计划列表,请使用$addToSet方法修改$group管道阶段以包含call_schedule列表:

var today = new Date();
var tomorrow = new Date(new Date().setDate(new Date().getDate()+1));

db.collection.aggregate([
    {
        "$match": {
            "call_schedule.callDate": {
                "$gte": today,
                "$lt": tomorrow
            }
        }
    },
    {
        "$unwind": "$call_schedule"
    },
    {
        "$match": {
            "call_schedule.callDate": {
                "$gte": today,
                "$lt": tomorrow
            }
        }
    },
    {
        "$group": {
            "_id": {
                "userId": "$userId"
            },
            "call_list": {
                "$addToSet": "$call_schedule"
            }
        }
    }  
]);

样本集合包含以下文件(其中包括今天及之前的3个通话时间表,即2015-04-29:

/* 0 */
{
    "_id" : ObjectId("553799187174b8c402151d06"),
    "modified" : ISODate("2015-04-22T12:50:31.477Z"),
    "name" : "Suresh",
    "userId" : "sursha6398",
    "created" : ISODate("2015-04-22T12:50:32.457Z"),
    "deleted" : false,
    "call_schedule" : [ 
        {
            "callType" : "a",
            "callDate" : ISODate("2015-01-14T18:30:00.000Z"),
            "_id" : ObjectId("553799187174b8c402151d0e")
        }, 
        {
            "callType" : "a",
            "callDate" : ISODate("2015-01-31T18:30:00.000Z"),
            "_id" : ObjectId("553799187174b8c402151d0d")
        }, 
        {
            "callType" : "b",
            "callDate" : ISODate("2015-02-19T18:30:00.000Z"),
            "_id" : ObjectId("553799187174b8c402151d0c")
        }, 
        {
            "callType" : "b",
            "callDate" : ISODate("2015-03-02T18:30:00.000Z"),
            "_id" : ObjectId("553799187174b8c402151d0b")
        }, 
        {
            "callType" : "c",
            "callDate" : ISODate("2015-03-17T18:30:00.000Z"),
            "_id" : ObjectId("553799187174b8c402151d0a")
        }, 
        {
            "callType" : "b",
            "callDate" : ISODate("2015-04-29T18:30:00.000Z"),
            "_id" : ObjectId("553799187174b8c402151d09")
        }, 
        {
            "callType" : "a",
            "callDate" : ISODate("2015-04-29T18:30:00.000Z"),
            "_id" : ObjectId("553799187174b8c402151d08")
        }, 
        {
            "callType" : "d",
            "callDate" : ISODate("2015-04-29T18:30:00.000Z"),
            "_id" : ObjectId("553799187174b8c402151d07")
        }
    ]
}

以上聚合将给出输出:

/* 0 */
{
    "result" : [ 
        {
            "_id" : {
                "userId" : "sursha6398"
            },
            "call_list" : [ 
                {
                    "callType" : "d",
                    "callDate" : ISODate("2015-04-29T18:30:00.000Z"),
                    "_id" : ObjectId("553799187174b8c402151d07")
                }, 
                {
                    "callType" : "a",
                    "callDate" : ISODate("2015-04-29T18:30:00.000Z"),
                    "_id" : ObjectId("553799187174b8c402151d08")
                }, 
                {
                    "callType" : "b",
                    "callDate" : ISODate("2015-04-29T18:30:00.000Z"),
                    "_id" : ObjectId("553799187174b8c402151d09")
                }
            ]
        }
    ],
    "ok" : 1
}