当前位置: 代码迷 >> 综合 >> Mongoose - mapReduce
  详细解决方案

Mongoose - mapReduce

热度:62   发布时间:2024-01-15 11:46:46.0

使用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) 
  相关解决方案