I'm new to mongoDB, the collection I'm working with was constructed by a python file that accesses my gmail using the gmail API.I'm trying to make a query to find the time of each day of the week with the highest number of received emails. Here was my attempt:
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}}}}])
Here is what an email object looks like:
{
"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
}
The date of the email is in the payload.headers portion of the object where "name" is date and "value" is the date in ISODate format. The query works without the $max command and gives the number of emails per hour out of all emails. Once I put the $max command it out puts: { "_id" : hour, "total" : 1 }
for every hour.