Skip to content

Instantly share code, notes, and snippets.

@xseignard
Created December 18, 2012 04:24
Show Gist options
  • Save xseignard/4324997 to your computer and use it in GitHub Desktop.
Save xseignard/4324997 to your computer and use it in GitHub Desktop.
Q2 from M101 final exam
// requires
var MongoClient = require('mongodb').MongoClient;
// mongodb uri
var uri = 'mongodb://localhost:27017/enron';
MongoClient.connect(uri, function(err, db) {
console.log('Connecting to the db...');
if (err) {
console.log('Error during connection. Aborting.');
process.exit(1);
}
console.log('Connected');
db.collection('messages', function(err,collection) {
collection.aggregate([
{ $match: {'headers.From':{$in:['[email protected]','[email protected]','[email protected]','[email protected]']}}}
,{ $project: {'from':'$headers.From','to':'$headers.To'}}
,{ $unwind : '$to' }
,{ $group : {
_id: {from: '$from', to: '$to'},
count: {$sum:1}
}
}
,{ $sort: {count:1}}
],
function(err, result) {
console.log(result);
db.close();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment