【发布时间】:2026-01-20 04:25:01
【问题描述】:
我正在尝试计算每个地区的学生人数。 我有一个看起来像
的 模型var mongoose = require('mongoose');
var schema = mongoose.Schema;
var studentSchema = new mongoose.Schema(
{
"name":String,
"address" :{
"locality":String
}
});
module.exports = mongoose.model('Student', studentSchema);
然后我有一些 Node.js 代码
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)
})
};
我得到的错误是。关于我可能做错的任何提示?
类型错误:
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)
【问题讨论】:
标签: node.js mongodb mongoose mapreduce