Last active
November 20, 2016 22:30
-
-
Save adispen/6860f70f3bd2de3aef873fac5afcb93e to your computer and use it in GitHub Desktop.
MongoProj Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To find counts of days of the week use this | |
// getUTCDay returns a Day (0-6) see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date for more | |
// Examples: getUTCMonth for Months (0-11), getUTCHours (0-23) | |
db.gmail_data.mapReduce( | |
function() { | |
var dateDoc; | |
for( var i = 0; i < this.payload.headers.length; i++ ) { | |
var currDoc = this.payload.headers[i]; | |
if (currDoc.name === 'Date') { | |
dateDoc = currDoc; | |
break; | |
} | |
} | |
if (!dateDoc) { | |
emit(null, 1); | |
return; | |
} | |
var parsedDate = new Date(dateDoc.value); | |
emit(parsedDate.getUTCDay(), 1); | |
}, | |
function(key, values) { return Array.sum(values) }, | |
{ out: { inline: 1 } } | |
); | |
// Returns the top 10 'FROM' fields, sorted | |
db.gmail_data.aggregate([ | |
{ $group: | |
{ count: { $sum: 1 }, | |
_id: { | |
$let: { | |
vars: { 'from': { | |
$arrayElemAt: [ | |
{ $filter: { | |
input: '$payload.headers', as: 'header', cond: { $eq: [ '$$header.name', 'From' ] } } | |
}, 0 ] | |
} }, | |
in: '$$from.value' | |
} | |
} | |
} | |
}, | |
{ $sort: { count: -1 } }, | |
{ $limit: 10 } | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment