【问题标题】:Mongo aggregate count instances of valueMongo聚合计数值实例
【发布时间】:2017-04-14 14:53:15
【问题描述】:

我有一组 (~35k) 文档,如下所示:

{
    "_id" : ObjectId("583dabfc7572394f93ac6ef2"),
    "updatedAt" : ISODate("2016-11-29T16:25:32.130Z"),
    "createdAt" : ISODate("2016-11-29T16:25:32.130Z"),
    "sourceType" : "report",
    "sourceRef" : ObjectId("583da865686e3dfbd977f059"),
    "type" : "video",
    "caption" : "lorem ipsum",
    "timestamps" : {
        "postedAt" : ISODate("2016-08-26T15:09:35.000Z"),
        "monthOfYear" : 7, // 0-based
        "dayOfWeek" : 5, // 0-based
        "hourOfDay" : 16 // 0-based
    },
    "stats" : {
        "comments" : 0,
        "likes" : 8
    },
    "user" : {
        "id" : "123456",
        "username" : "johndoe",
        "fullname" : "John",
        "picture" : ""
    },
    "images" : {
        "thumbnail" : "",
        "low" : "",
        "standard" : ""
    },
    "mentions" : [
        "janedoe"
    ],
    "tags" : [ 
        "holiday", 
        "party"
    ],
    "__v" : 0
}

我想生成一份汇总报告,该报告将用于按一天中的小时/一周中的一天/一年中的月份绘制文档频率,以及提及/标签的计数。

{
  // Each frequency is independant from the others,
  // e.g. the total count for each frequency should
  // be ~35k.
  dayFrequency: [
    { day: 0, count: 1400 }, // Monday
    { day: 1, count: 1700 }, // Tuesday
    { day: 2, count: 1800 }, // Wednesday
    { /* etc */ },
    { day: 6, count: 1200 }  // Sunday
  ],

  monthFrequency: [
    { month: 0, count: 200 }, // January
    { month: 1, count: 250 }, // February
    { month: 2, count: 300 }, // March
    { /* etc */ },
    { month: 11, count: 150 } // December
  ],

  hourFrequency: [
    { hour: 0, count: 150 }, // 0am
    { hour: 1, count: 200 }, // 1am
    { hour: 2, count: 275 }, // 2am
    { /* etc */ },
    { hour: 23, count: 150 }, // 11pm
  ],

  mentions: {
    janedoe: 12,
    johnsmith: 11,
    peter: 54,
    /* and so on */
  },

  tags: {
    holiday: 872,
    party: 1029,
    /* and so on */
  }
}

这可能吗?如果可以,我会怎么写?据我了解,当我正在执行所有匹配文档的聚合时,它实际上是一个组?

到目前为止,我的代码只是将所有匹配的记录归为一组,但我不确定如何继续前进。

Model.aggregate([
  { $match: { sourceType: 'report', sourceRef: '583da865686e3dfbd977f059' } },
  { $group: { 
    _id: '$sourceRef'
  }}
], (err, res) => {
  console.log(err);
  console.log(res);
})

也可以将频率计数为计数数组(例如[ 1400, 1700, 1800, /* etc */ 1200 ]),这使我查看$count 和其他一些运算符,但是我再次不清楚用法。

【问题讨论】:

    标签: javascript mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    目前(在撰写本文时)无法在单个管道中使用 MongoDB 3.2 执行此操作。但是,从 MongoDB 3.4 起,您可以使用 $facet 运算符,该运算符允许在同一输入文档集的单个阶段内处理多个聚合管道。每个子管道在输出文档中都有自己的字段,其结果存储为文档数组。

    例如,上面可以通过运行以下聚合管道来实现:

    Model.aggregate([
        { "$match": { "sourceType": "report", "sourceRef": "583da865686e3dfbd977f059" } },
        {
            "$facet": {
                "dayFrequency": [
                    {
                        "$group": {
                            "_id": "$timestamps.dayOfWeek",
                            "count": { "$sum": 1 }
                        }
                    }
                ],
                "monthFrequency": [
                    {
                        "$group": {
                            "_id": "$timestamps.monthOfYear",
                            "count": { "$sum": 1 }
                        }
                    }
                ],
                "hourFrequency": [
                    {
                        "$group": {
                            "_id": "$timestamps.hourOfDay",
                            "count": { "$sum": 1 }
                        }
                    }
                ],
                "mentions": [
                    { "$unwind": "$mentions" },
                    {
                        "$group": {
                            "_id": "$mentions",
                            "count": { "$sum": 1 }
                        }
                    }
                ],
                "tags": [
                    { "$unwind": "$tags" },
                    {
                        "$group": {
                            "_id": "$tags",
                            "count": { "$sum": 1 }
                        }
                    }
                ]
            }
        }
    ], (err, res) => {
        console.log(err);
        console.log(res);
    })
    

    【讨论】:

      猜你喜欢
      • 2014-10-04
      • 1970-01-01
      • 2013-11-23
      • 2013-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多