为一个排名algorithm分页mongoosemapReduce

我正在使用MongoDB mapReduce编码一个排名饲料algorithm,它几乎可以工作,但最新的实施是分页。 地图减less支持结果的限制,但我怎么能实现偏移(跳过),例如根据结果的最新查看_ID,知道我正在使用mongoose?

这是我写的程序:

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它不是要走的路,你build议其他如何实现这样的东西?

你需要的是一个页面分隔符,这不是你所说的最近查看的id,而是你的sorting属性。 在这种情况下,似乎是公式Math.log(this.likes + this.comments + 1) / Math.LN10 / Math.abs((now - this.createdAt) / 6e7 + 1)

所以,在你的mapReduce query需要保存上面那个公式的where值。 或者具体来说,“公式”= . And also it needs to hold the value of createdAt at the last page, since you don't sort by that. (Assuming createdAt is unique). So your . And also it needs to hold the value of createdAt at the last page, since you don't sort by that. (Assuming createdAt is unique). So your . And also it needs to hold the value of createdAt at the last page, since you don't sort by that. (Assuming createdAt is unique). So your of mapReduce would say . And also it needs to hold the value of createdAt at the last page, since you don't sort by that. (Assuming createdAt is unique). So your查询of mapReduce would say :whereFormulaExpression,createdAt:{$ lt:lastCreatedAt}`

如果你允许多个完全相同的createdAt值,你必须在数据库本身之外进行一些操作。

所以你只是通过公式search。

理想情况下,这给了你一个具有该值的元素,并在下一个元素之后进行sorting。 所以在回复模块调用者的时候,把这个第一个元素从数组中删除(并且确保你实际上需要更多的结果,那么你需要这样做)。

现在,由于您允许多个相似的值,您需要另一个标识的道具,比如object id或created_at。 您的消费者(此模块的调用者)将不得不提供( last value of the score createdAt of the last object )。 假设你有一个页面正好在中间分割 – 一个或多个对象在前一页,另一个在下一个。 您不得不简单地删除最高值(因为相同的分数已经在上一页中提供),但是可能有几个从顶部开始。

然后它变得非常疯狂,因为可能你的整个页面已经被提供 – 比较_ids,寻找你的模块调用者为你提供的第一个页面。 或者查看数据并确定有多less匹配值,请尝试从mapReduce中获取至less更多的值,然后获得实际页面大小。

除此之外,我会用聚合的方式来做这件事,它应该更加重要。