Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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
Node.js MongoDB聚合函数,用于返回特定日期范围的按天计数_Node.js_Mongoose_Aggregation Framework - Fatal编程技术网

Node.js MongoDB聚合函数,用于返回特定日期范围的按天计数

Node.js MongoDB聚合函数,用于返回特定日期范围的按天计数,node.js,mongoose,aggregation-framework,Node.js,Mongoose,Aggregation Framework,我需要得到一个特定日期范围内每天的个人用户数。比如说,在1号到30号的一个月内,总共有100个用户,我需要得到这样的数量 { 1st - 2 users 2nd - 10 users } MessagesSchema.statics.totalMessagesGraph = (id, startDate, endDate, platform) => { return Messages.aggregate([ { $match

我需要得到一个特定日期范围内每天的个人用户数。比如说,在1号到30号的一个月内,总共有100个用户,我需要得到这样的数量

{
    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之后应该进行什么查询。如果可能,请获取一个样本数据集并提供输出。

使用$group获取逐日计数 比如说

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
}
...
您可以使用上述查询结果获得所需的输出

更准确地说,您可以从组查询中删除月份和年份参数,以获得如下输出:

{
    "_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文档,也可以参考此文档

希望上面的示例能帮助您获得所需的输出。

使用$group获得每日计数 比如说

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
}
...
您可以使用上述查询结果获得所需的输出

更准确地说,您可以从组查询中删除月份和年份参数,以获得如下输出:

{
    "_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文档,也可以参考此文档


希望以上示例能帮助您获得所需的输出。

以下是我经过几次尝试后得出的解决方案

            {
                '$project': {
                    timestamp: {'$dateToString': {format: '%Y-%m-%d', date: '$timestamp'}}                }
            }, {
                '$group': {
                    _id: {timestamp: '$timestamp'},
                    count: {'$sum': 1}
                }
            }
这是输出

  "response": [
    {
      "_id": {
        "timestamp": "2019-01-08"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-01-13"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-01-16"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-01-17"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-01-19"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-02-01"
      },
      "count": 1
    }
  ]

这是我经过几次尝试后得出的解决方案

            {
                '$project': {
                    timestamp: {'$dateToString': {format: '%Y-%m-%d', date: '$timestamp'}}                }
            }, {
                '$group': {
                    _id: {timestamp: '$timestamp'},
                    count: {'$sum': 1}
                }
            }
这是输出

  "response": [
    {
      "_id": {
        "timestamp": "2019-01-08"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-01-13"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-01-16"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-01-17"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-01-19"
      },
      "count": 1
    },
    {
      "_id": {
        "timestamp": "2019-02-01"
      },
      "count": 1
    }
  ]

@阿德尔:谢谢你的编辑。我很着急。抱歉。@Adeel谢谢您的编辑。我很着急。抱歉。感谢@arpan提供的解决方案。但这并不是我所期望的结果。经过几次试验后,在同一天得到了解决方案。请查看我在下面发布的解决方案,你可以用我考虑过的两种方式中的任何一种,比如第1天:20,第2天:30…谢谢你的解决方案@arpan。但这并不是我所期望的结果。经过几次试验后,在同一天得到了解决方案。请查看我在下面发布的解决方案,你可以用我考虑过的两种方式中的任何一种,比如第1天:20,第2天:30。。。