php - MongoDB 选择排序问题

标签 php android mysql sql mongodb

我的问题是我想做一个数据库查找(选择)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/

相关文章:

php - 无法将希伯来语发布到 mysql

php - 防止SQL注入(inject): Do I need to escape input before binding values into a PDO query

php - 在 Android 平板电脑 webview 中嵌入 Youtube 视频

php - 如何使用提交按钮的单个 onclick 事件触发 href 和 php 代码

php - 如何在 Eloquent 模型中创建复杂的虚拟列?

php - PHP 图片上传问题

android - 在 Retrofit 回调中访问原始响应体

java - 我的导航栏上的第一个图标是蓝色的,并且比其他图标大,我该如何更改它?

java - 使用 Spring Boot 测试 OneToMany 与 Postman 的关系时出错

php - 显示数据库中最接近的匹配项