我需要为 8 个用户中的每一个获取前 3 个结果。目前我正在为每个用户循环并进行 8 次调用 db。有没有办法构造查询以在单个数据库拉取中提取相同的 8X3 数据集?
selected_users = users.sample(8)
cur = 0
while cur <= selected_users .count-1
cursor = status_store.find({'user' => selected_users[cur]},{:fields =>params}).sort('score', -1).limit(3)
*do something*
cur+=1
end
我从中提取的集合如下所示。每个用户都可以拥有无限数量的推文,因此我没有将它们嵌入到用户文档中。
{
"_id" : ObjectId("51e92cc8e1ce7219e40003eb"),
"id_str" : "57915476419948544",
"score" : 904,
"text" : "Yesterday we had a bald eagle on the show. Oddly enough, he was in the country illegally.",
"timestamp" : "19/07/2013 08:10",
"user" : {
"id_str" : "115485051",
"name" : "Conan O'Brien",
"screen_name" : "ConanOBrien",
"description" : "The voice of the people. Sorry, people.",
}
}
提前致谢。