问题描述
我需要的:
假设你使用MongoDB,你有一个集合,名为 users
,每个用户都有一个跟随数组,其用户
_id
s。然后,您还有另一个集合 statuses
,每个状态包含其作者的 _id
。
如何向特定用户显示他所追踪的所有状态?
Suppose you're using MongoDB and you have a collection called users
, and each user has a "following" array with user _id
s of the people he's following. Then you have another collection statuses
, with each status containing the _id
of its author.How do you display to a certain user all the statuses added by people he's following?
我试过的内容:
我将当前用户在数组中使用的所有用户(code> _id s(我正在使用PHP),然后我用它来查找那些用户在中使用 $的所有状态。
I put all the users _id
s that the current user is following in an array (I'm using PHP), then I used it to find all the statuses by those users using $in
.
问题:
这是最好的解决方案吗?
Is this the best solution?
推荐答案
我看不到任何其他方式,我以前实现了这样的事情,没有问题。
I can't see any other way too, i implemented such thing before and didn't have a problem.
在你的情况下,应该是这样的,你通过某些用户的 $ follower_ids
数组作为参数您的功能:
On your case, it should be sth like this, you pass certain user's $follower_ids
array as an argument to your function:
$query = array("status_owner_id" => array('$in' => $follower_ids));
$cursor = $mongo->yourdb->statuses->find($query);
如果你索引状态(如果你有足够的ram这样做)在owner_id你会得到结果真的很快。
And if you index statuses (if you have enough ram to do so) upon owner_id you'd get the results really fast.
希望它有帮助,Sinan。
Hope it helps, Sinan.
这篇关于与MongoDB制作类似Twitter的时间表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!