Skip to content

Instantly share code, notes, and snippets.

@PierrickP
Last active December 16, 2015 14:39
Show Gist options
  • Save PierrickP/5450628 to your computer and use it in GitHub Desktop.
Save PierrickP/5450628 to your computer and use it in GitHub Desktop.
var Mongo = require('mongodb');
var fs = require('fs'),
async = require('async');
Mongo.connect("mongodb://localhost:27017/aggregation_test?w=1", function(err, db) {
var collection = db.collection('tree');
var time = Date.now();
collection.aggregate(
{
$project : {
genre: 1
}
}, {
$group : {
_id: '$genre',
totalByGenre: { $sum: 1}
}
},{
$sort: {totalByGenre: -1}
}, function(err, result) {
console.log('Time Elapse %d ms', Date.now() - time); // env 350ms
console.log(result);
db.close();
});
});
var Mongo = require('mongodb');
var fs = require('fs'),
async = require('async');
Mongo.connect("mongodb://localhost:27017/aggregation_test?w=1", function(err, db) {
var collection = db.collection('tree');
var time = Date.now();
collection.group(
{
genre: true
},
{},
{
totalByGenre: 0
},
function ( curr, result ) {
result.totalByGenre += 1;
},
function(err, result) {
console.log('Time Elapse %d ms', Date.now() - time); // env 3500ms
console.log(result);
db.close();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment