使用mongoose中的mapReduce方法统计结果。
示例,以审方订单中药师为key进行统计,统计出每个药师的审方订单数量。
var o = {};// `map()` and `reduce()` are run on the MongoDB server, not Node.js,// these functions are converted to stringso.query = {pharmacist: {$nin:[null]}};o.map = function () { emit(this.pharmacist, 1) };o.reduce = function (k, vals) { return Array.sum(vals); };// o.sort = {value: -1}; 不起作用,不能按照value排序,原因待查Order.mapReduce(o, function (err, analysis) {if (err) {return next(err);} else {console.info(analysis);}})
为了能够对结果进行排序,改为下列写法
var o = {};// You can also define `map()` and `reduce()` as strings if your// linter complains about `emit()` not being definedo.query = {pharmacist: {$nin:[null]}};o.map = function () { emit(this.pharmacist, 1)