【发布时间】:2016-04-17 07:09:15
【问题描述】:
我正在使用 MongoDB mapReduce 来编写排名提要算法,它几乎可以工作,但最新实现的是分页。 map reduce 支持结果限制,但我如何实现基于偏移(跳过),例如在最新查看的_id 上,知道我在使用猫鼬吗?
这是我写的程序:
o = {};
o.map = function() {
//log10(likes+comments) / elapsed hours from the post creation
emit(Math.log(this.likes + this.comments + 1) / Math.LN10 / Math.abs((now - this.createdAt) / 6e7 + 1), this);
};
o.reduce = function(key, values) {
//sort the values, when they have the same score
values.sort(function(a, b) {
a.createdAt - b.createdAt;
});
//serialize the values, because mongoose does not support multiple returned values
return JSON.stringify(values);
};
o.scope = {now: new Date()};
o.limit = 15;
Posts.mapReduce(o, function(err, results) {
if (err) return console.log(err);
console.log(results);
});
另外,如果 mapReduce 这不是要走的路,你是否建议其他关于如何实现这样的东西?
【问题讨论】:
标签: node.js mongodb mongoose pagination mapreduce