Skip to content

Instantly share code, notes, and snippets.

@ronalstal
Created November 6, 2012 20:01
Show Gist options
  • Save ronalstal/4027122 to your computer and use it in GitHub Desktop.
Save ronalstal/4027122 to your computer and use it in GitHub Desktop.
MongoDB course m0101 homework 2-2 for node using mapReduce
var mongodb = require('mongodb');
var _ = require('underscore');
var db = mongodb.Db('students', new mongodb.Server('localhost', 27017), {safe: true});
db.open(function(err, client) {
if(err) {
console.log('Error opening connection' + err);
}
console.log('db opened');
var Grades = new mongodb.Collection(client, 'grades');
console.log('Grades collection');
Grades.count(function(err, cnt) {
if (err) {
console.log(err);
}
console.log('There are ' + cnt
+ ' records in Grades');
}); // count result
var qry = {"type":"homework"};
var mapf = function() {
emit( this.student_id,
{
student_id: this.student_id,
lowGrade: this.score,
docid: this._id
});
};
var reducef = function(key, values) {
var r = {
student_id: key,
lowGrade: null,
docid: null
}
var lg = null, di = null;
values.forEach(function(v) {
if ( v.lowGrade < lg || lg == null) {
lg = v.lowGrade;
di = v.docid;
}
});
r.lowGrade = lg;
r.docid = di;
return r;
};
Grades.mapReduce(mapf, reducef,
{
query: qry,
out: { reduce: "lowest_grades" },
},
function(err, rsltColl) {
if (err) {
console.log(err);
}
console.log('mapReduce callback...');
rsltColl.count(function(err, cnt) {
if (err) {
console.log(err);
}
console.log('There are ' + cnt
+ ' records in lowest_grades');
rsltColl.find().toArray(function(err, delDocs) {
if (err) {
console.log(err);
}
console.log('start looping results...');
var nbDel = 0;
_.each(delDocs, function(iter) {
nbDel++;
console.log(nbDel+": "+iter.value.docid);
Grades.remove({"_id":iter.value.docid}, {safe:true}, function(err, nbRemoved) {
if (err) {
console.log(err);
}
console.log(nbRemoved + ' records removed');
}) // remove
}); // each delDocs
client.close();
console.log('done removing '+nbDel+' records');
}); // find delDocs
}); // count result
} // mapReduce callback
); // mapReduce()
}); // db.open()
var mongodb = require('mongodb');
var _ = require('underscore');
var db = mongodb.Db('students', new mongodb.Server('localhost', 27017), {safe: true});
db.open(function(err, client) {
if(err) {
console.log('Error opening connection' + err);
}
console.log('db opened');
var Grades = new mongodb.Collection(client, 'grades');
console.log('Grades collection');
// count Grades before mapReduce (should be 800)
Grades.count(function(err, cnt) {
if (err) {
console.log(err);
}
console.log('There are ' + cnt
+ ' records in Grades');
}); // count Grades before mapReduce
// set up the stuff for mapReduce:
// only act on documents with type=homework
var qry = {"type":"homework"};
// per student map the score and the _id of the document
var mapf = function() {
emit( this.student_id,
{
student_id: this.student_id,
lowGrade: this.score,
docid: this._id
});
};
// reduce this to the lowest score per student
var reducef = function(key, values) {
var r = {
student_id: key,
lowGrade: null,
docid: null
}
var lg = null, di = null;
values.forEach(function(v) {
if ( v.lowGrade < lg || lg == null) {
lg = v.lowGrade;
di = v.docid;
}
});
r.lowGrade = lg;
r.docid = di;
return r;
};
// now remove this document with the lowest scrore from Grades
// THIS DOES *NOT* WORK, MONGO DOES NOT ALLOW IT
var finalizef = function(key, value) {
Grades.remove({"_id":value.docid}, {safe:true}, function(err, nbRemoved) {
if (err) {
console.log(err);
}
});
};
// call it
Grades.mapReduce(mapf, reducef,
{
query: qry,
out: { reduce: "lowest_grades" },
finalize: finalizef
},
function(err, rsltColl) {
if (err) {
console.log(err);
}
console.log('mapReduce callback...');
// count the result (should be 200)
rsltColl.count(function(err, cnt) {
if (err) {
console.log(err);
}
console.log('There are ' + cnt
+ ' records in lowest_grades');
// count the remaining grades (should be 600)
Grades.count(function(err, cnt) {
if (err) {
console.log(err);
}
console.log('There are now' + cnt
+ ' records in Grades');
}); // count Grades after
}); // count result
} // mapReduce callback
); // mapReduce()
}); // db.open()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment