gpt4 book ai didi

javascript - Mongo 聚合计数值实例

转载 作者:太空宇宙 更新时间:2023-11-04 16:18:13 25 4
gpt4 key购买 nike

我有一组(~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,/* 等 */1200 ]),这让我看看 $count 和其他一些运算符,但是我再次不清楚用法。

最佳答案

目前(在撰写本文时)不可能在单个管道中使用 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);
})

关于javascript - Mongo 聚合计数值实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40885723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com