我的问题是我想做一个数据库查找(选择)witz 分类。 ^^
在我的代码中看起来像这样:
$return = $db->selectCollection( $category )->find(array("time" > $time_lastweek))->sort(array("rating/count_raitings" => 1,"count_raitings" => 1))->limit(10);
我以前在 SQL (PDO) 中这样做过:
$last_week = $dbh->prepare('SELECT * FROM '.$category.' WHERE time > :zeit ORDER BY rating/count_raitings ASC, count_raitings ASC LIMIT 10');
$last_week->execute(array(':zeit' => $time_lastweek));
$return = $last_week->fetchAll(PDO::FETCH_CLASS);
谁能帮帮我。 MongoDB 对我不起作用。
最佳答案
以下是您可以如何使用 aggregation framework在 shell 中执行此操作(应该很容易转换为 PHP):
db.category.aggregate([
// Filter the docs to those where time > time_lastweek
{ $match: {time: {$gt: time_lastweek}}},
// Identify what to include from each doc, adding a computed field for the division
{ $project: {
time: 1,
rating: 1,
count_raitings: 1,
div_val: {$divide: ['$rating', '$count_raitings']}
} },
// Sort the results
{ $sort: {div_val: 1, count_raitings: 1}},
// Limit to the top 10
{ $limit: 10 }
])
关于php - MongoDB 选择排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13390673/