MongoDB的聚合函数为特定日期范围返回天算明智
我需要得到个人用户的计数特定日期范围太每天的基础上。比方说,有一个月内共有100个用户的是:(1日 - 30日),我需要得到像计数
{
1st - 2 users
2nd - 10 users
}
MessagesSchema.statics.totalMessagesGraph = (id, startDate, endDate, platform) => {
return Messages.aggregate([
{
$match: {
id: id,
platform: platform,
timestamp: {
$gte: new Date(startDate),
$lte: new Date(endDate)
}
}
}
])
}
应该是什么在这里得到期望的结果?
预期结果:
对于特定日期范围内每一天的计数。
{
date1 - 20,
date2 - 22,
date3 - 24,
...
date30 - 12
}
预期的输出应该像上面。什么查询应该$match
后进行。如果可能的话,请采取样本数据集,并提供输出。
使用$组获得一天算明智例如
db.collection.aggregate([
{
$match: {
//id: id,
//platform: platform,
//timestamp: {
//$gte: new Date(startDate),
//$lte: new Date(endDate)
//}
//}
// Your matching logic
},
/* Now grouping users based on _id or id parameter for each day
from the above match results.
$createdAt can be replaced by date property present in your database.
*/
{ $group : {
id : { day: { $dayOfMonth: "$createdAt" },
month: { $month: "$createdAt" },
year: { $year: "$createdAt" } },
count : {$sum : 1}
}
}
])
在此基础上,你会得到输出如下:
{
"_id" : {
"day" : 14,
"month" : 1,
"year" : 2017
},
"count" : 2.0
}
/* 2 */
{
"_id" : {
"day" : 31,
"month" : 1,
"year" : 2017
},
"count" : 8.0
}
/* 3 */
{
"_id" : {
"day" : 2,
"month" : 1,
"year" : 2017
},
"count" : 4.0
}
...
您可以使用上面的查询结果来获得所需的输出。
更准确地说,你可以从组查询删除月份和年份参数,以获得输出,如:
/* 1 */
{
"_id" : {
"day" : 25
},
"count" : 7.0
}
/* 2 */
{
"_id" : {
"day" : 18
},
"count" : 4.0
}
/* 3 */
{
"_id" : {
"day" : 17
},
"count" : 4.0
}
...
仅供参考,您可以检查MongoDB的文档也提到这一点。
MongoDB Aggregation Queries for "Counts Per Day"
希望上面的例子帮助您获得所需的输出。
MongoDB的聚合函数为特定日期范围返回天算明智
我需要得到个人用户的计数特定日期范围太每天的基础上。比方说,有一个月内共有100个用户的是:(1日 - 30日),我需要得到像计数
{
1st - 2 users
2nd - 10 users
}
MessagesSchema.statics.totalMessagesGraph = (id, startDate, endDate, platform) => {
return Messages.aggregate([
{
$match: {
id: id,
platform: platform,
timestamp: {
$gte: new Date(startDate),
$lte: new Date(endDate)
}
}
}
])
}
应该是什么在这里得到期望的结果?
预期结果:
对于特定日期范围内每一天的计数。
{
date1 - 20,
date2 - 22,
date3 - 24,
...
date30 - 12
}
预期的输出应该像上面。什么查询应该$match
后进行。如果可能的话,请采取样本数据集,并提供输出。
使用$组获得一天算明智例如
db.collection.aggregate([
{
$match: {
//id: id,
//platform: platform,
//timestamp: {
//$gte: new Date(startDate),
//$lte: new Date(endDate)
//}
//}
// Your matching logic
},
/* Now grouping users based on _id or id parameter for each day
from the above match results.
$createdAt can be replaced by date property present in your database.
*/
{ $group : {
id : { day: { $dayOfMonth: "$createdAt" },
month: { $month: "$createdAt" },
year: { $year: "$createdAt" } },
count : {$sum : 1}
}
}
])
在此基础上,你会得到输出如下:
{
"_id" : {
"day" : 14,
"month" : 1,
"year" : 2017
},
"count" : 2.0
}
/* 2 */
{
"_id" : {
"day" : 31,
"month" : 1,
"year" : 2017
},
"count" : 8.0
}
/* 3 */
{
"_id" : {
"day" : 2,
"month" : 1,
"year" : 2017
},
"count" : 4.0
}
...
您可以使用上面的查询结果来获得所需的输出。
更准确地说,你可以从组查询删除月份和年份参数,以获得输出,如:
/* 1 */
{
"_id" : {
"day" : 25
},
"count" : 7.0
}
/* 2 */
{
"_id" : {
"day" : 18
},
"count" : 4.0
}
/* 3 */
{
"_id" : {
"day" : 17
},
"count" : 4.0
}
...
仅供参考,您可以检查MongoDB的文档也提到这一点。
MongoDB Aggregation Queries for "Counts Per Day"
希望上面的例子帮助您获得所需的输出。