查找最新的&最近的帖子,最多20个 [英] Find most recent & closest posts, limit 20

查看:68
本文介绍了查找最新的&最近的帖子,最多20个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在MongoDB中有一堆帖子(用于提要,例如Twitter/Facebook/foursquare提要),每个帖子都有一个时间戳.

Let's say I have a bunch of posts (for a feed, like a Twitter/Facebook/foursquare feed) in MongoDB, and each post has a location & a timestamp.

获取最新&的最佳方法是什么?最近的帖子,最多20个帖子?

What's the best way to get the most recent & closest posts, limited to 20 posts?

这也是一个主观问题.假设您可以指定$maxDistance以及从现在开始的最长时间(我不确定您是怎么做的).您将如何指定它们?您是按最新的还是最接近的进行排序,还是将其随机化还是以其他方式进行排序?您认为哪种排序算法最有趣?

This is also a subjective question. Let's say that you can specify $maxDistance and the max time since now (I'm not sure how you'd do it otherwise.). How would you specify them? Would you sort by most recent or closest, or keep it random or sort some other way? Which sorting algorithm do you think is most interesting?

推荐答案

我想您最终会获得一个具有两个离散排名维度的帖子列表,即:

I suppose you ultimately end up with a list of posts that have two discrete ranking dimensions, i.e.:

{ age: 86400, distance: 1000 }
{ age: 172800, distance: 5000 }
{ age: 57600, distance: 20000 }
{ age: 288000, distance: 8000 }

单位是多少并不重要,可以说秒和米.如果您希望两者都影响排序等级,那么您最终会得到一种排名算法,最简单的方法是这样:

Doesn't really matter what the units are, lets say seconds and metres. If you want both to affect the sorting rank then you end up with a ranking algorithm, at its simplest something like this:

rank = (C1 * age) + (C2 * distance)

在C 1 和C 2 是常量的情况下,您可以进行调整以调整权重.这些值将取决于您使用的单位以及分配给每个维度的排名影响力.

Where C1 and C2 are constants you can tweak to tune the weightings. The values will depend what units you're using, and how much ranking influence you assign to each dimension.

另一种选择可能是先按时间总计排序,然后再按距离排序,因此从今天开始的所有帖子都按距离排序;其次是昨天的距离排序,依此类推.反之亦然,按距离范围排序,然后按年龄排序,因此所有(0-1000m)内均按年龄排序;其次是(1001-2000m)之内的所有东西,依此类推.

Another option could be ordering first by a time aggregate then distance, so all posts from today ordered by distance; followed by yesterday's ordered by distance, and so on. Or vice-versa, ordering by a distance range, then age, so all within (0 - 1000m) ordered by age; followed by all within (1001 - 2000m), and so on.

这篇关于查找最新的&最近的帖子,最多20个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆