您好所有我想要做的是让每个不同departmentType的计数:的MongoDB /猫鼬:不能把简单的MapReduce工作
fnMap = function() {
emit(this.departments.departmentType, {typeCount:1});
}
fnReduce = function(key, values) {
var result = {typeCount: 0};
values.forEach(function(value) {
result.typeCount += value.brandCount;
});
return result;
};
var command = {
mapreduce : "clients",
query : {"departments.departmentType": {$exists: true}},
map : fnMap.toString(),
reduce : fnReduce.toString(),
//sort: {"departments.departmentType":1},
out: {inline: 1}
};
mongoose.connection.db.executeDbCommand(command, function(err, dbres) {
});
当执行命令,dbres.documents [0 ] .results仅包含1个包含部门类型总数的项目,而不是每个部门类型的项目数量及其数量。
任何想法我做错了什么?
另外,当我取消注释SORT行时,出现错误“db assertion failure:could not create cursor over ...”,我相信字段名称是正确写入的。
什么是brandCount减少?看起来像一个错字。应该是typeCount –
我会添加更正mapReduce到我的答案,以防它有用,但你不需要mapReduce在这里。 –