I have written the following code to get the number of students per locality:
var mongoose = require('mongoose');
var schema = mongoose.Schema;
var studentSchema = new mongoose.Schema(
{
"name":String,
"address" :{
"locality":String
}
});
module.exports = mongoose.model('Student', studentSchema);
The following is the Node.js code:
var Student = require('../../../models/Student');
module.exports.getStudentsBasedOnLocality = function(){
var o = {};
o.map = function () {
emit(Student.address.locality, 1)
}
o.reduce = function (k, vals) {
return vals.length
}
Student.collection.mapReduce(o, function (err, results) {
if(err) throw err;
console.log(results)
})
};
After implementing the above code, I am getting the following error:
TypeError:
Cannot read property 'out' of undefined
at Collection.mapReduce (C:\***\node_modules\mongodb\lib\collection.js:2961:21)
at NativeCollection.(anonymous function) [as mapReduce] (C:\***\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:136:28)