mongoDB aggregate() 在 collection 封电子邮件 objects 中查找电子邮件时间
mongoDB aggregate() finding email times in a collection of email objects
我是 mongoDB 的新手,我正在使用的 collection 是由一个 python 文件构建的,该文件使用 gmail API.I 访问我的 gmail'我尝试查询以查找收到电子邮件数量最多的一周中每一天的时间。这是我的尝试:
db.gmail_data2.aggregate(
[{$unwind: '$payload.headers' },
{$match:{'payload.headers.name':'Date'}},
{$project:{email_hour:{$hour:"payload.headers.value"}}},
{$group:{_id:"$email_hour",total:{$max:{$sum:1}}}}])
这是电子邮件 object 的样子:
{
"id": string,
"threadId": string,
"labelIds": [
string
],
"snippet": string,
"historyId": unsigned long,
"internalDate": long,
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
"sizeEstimate": integer,
"raw": bytes
}
电子邮件的日期在 object 的 payload.headers 部分,其中 "name" 是日期,"value" 是 ISODate 格式的日期。该查询在没有 $max 命令的情况下工作,并给出所有电子邮件中每小时的电子邮件数量。一旦我输入 $max 命令,它就会输出:{ "_id" : hour, "total" : 1 }
每小时。
你需要做这样的事情。第一个 $group 阶段按日期和小时对电子邮件进行分组,同时计算每小时的电子邮件数,最后一个组按日期分组并选择每天的最大电子邮件数,同时将按小时计算的电子邮件数推入数组。 $project 阶段通过比较以前的电子邮件最大计数与数组的计数和 return 匹配值来过滤最大电子邮件小时行。如果你只想得到小时,你可以在最后添加另一个项目阶段。
aggregate(
[{
$unwind: '$payload.headers'
}, {
$match: {
'payload.headers.name': 'Date'
}
}, {
$group: {
_id: {
email_date: {
$dateToString: {
format: "%Y-%m-%d",
date: '$payload.headers.value'
}
},
email_hour: {
$hour: '$payload.headers.value'
}
},
count: {
$sum: 1
}
}
}, {
$group: {
_id: '$_id.email_date',
email_by_hour: {
$push: {
email_hour: '$_id.email_hour',
count: '$count'
}
},
max_count: {
$max: '$count'
}
}
}, {
$project: {
_id: 0,
email_date: '$_id',
max_email_hour: {
$filter: {
input: '$email_by_hour',
as: 'item',
cond: {
$eq: ['$$item.count', '$max_count']
}
}
}
}
}])
示例输出:
{ "email_date" : "2016-11-21", "max_email_hour" : [ { "email_hour" : 1, "count" : 3 } ] }
{ "email_date" : "2016-11-20", "max_email_hour" : [ { "email_hour" : 12, "count" : 2 } ] }
我是 mongoDB 的新手,我正在使用的 collection 是由一个 python 文件构建的,该文件使用 gmail API.I 访问我的 gmail'我尝试查询以查找收到电子邮件数量最多的一周中每一天的时间。这是我的尝试:
db.gmail_data2.aggregate(
[{$unwind: '$payload.headers' },
{$match:{'payload.headers.name':'Date'}},
{$project:{email_hour:{$hour:"payload.headers.value"}}},
{$group:{_id:"$email_hour",total:{$max:{$sum:1}}}}])
这是电子邮件 object 的样子:
{
"id": string,
"threadId": string,
"labelIds": [
string
],
"snippet": string,
"historyId": unsigned long,
"internalDate": long,
"payload": {
"partId": string,
"mimeType": string,
"filename": string,
"headers": [
{
"name": string,
"value": string
}
],
"body": users.messages.attachments Resource,
"parts": [
(MessagePart)
]
},
"sizeEstimate": integer,
"raw": bytes
}
电子邮件的日期在 object 的 payload.headers 部分,其中 "name" 是日期,"value" 是 ISODate 格式的日期。该查询在没有 $max 命令的情况下工作,并给出所有电子邮件中每小时的电子邮件数量。一旦我输入 $max 命令,它就会输出:{ "_id" : hour, "total" : 1 }
每小时。
你需要做这样的事情。第一个 $group 阶段按日期和小时对电子邮件进行分组,同时计算每小时的电子邮件数,最后一个组按日期分组并选择每天的最大电子邮件数,同时将按小时计算的电子邮件数推入数组。 $project 阶段通过比较以前的电子邮件最大计数与数组的计数和 return 匹配值来过滤最大电子邮件小时行。如果你只想得到小时,你可以在最后添加另一个项目阶段。
aggregate(
[{
$unwind: '$payload.headers'
}, {
$match: {
'payload.headers.name': 'Date'
}
}, {
$group: {
_id: {
email_date: {
$dateToString: {
format: "%Y-%m-%d",
date: '$payload.headers.value'
}
},
email_hour: {
$hour: '$payload.headers.value'
}
},
count: {
$sum: 1
}
}
}, {
$group: {
_id: '$_id.email_date',
email_by_hour: {
$push: {
email_hour: '$_id.email_hour',
count: '$count'
}
},
max_count: {
$max: '$count'
}
}
}, {
$project: {
_id: 0,
email_date: '$_id',
max_email_hour: {
$filter: {
input: '$email_by_hour',
as: 'item',
cond: {
$eq: ['$$item.count', '$max_count']
}
}
}
}
}])
示例输出:
{ "email_date" : "2016-11-21", "max_email_hour" : [ { "email_hour" : 1, "count" : 3 } ] }
{ "email_date" : "2016-11-20", "max_email_hour" : [ { "email_hour" : 12, "count" : 2 } ] }