node.js - 如何用 Mongoose 对两个字段进行排序?

标签 node.js mongodb sorting mean database

我正在尝试让用户看到热门帖子。总体思路是按最新帖子排序 (_id: -1),然后按最多赞成票排序 (upvotes_count: -1),然后限制结果 (.limit(3))。这有点简化,所以请忽略这个“热门帖子”的实现。

不幸的是,我无法按照我想要的方式返回两种排序。因此,对于六个帖子的集合,它会返回最近的三个帖子,但不会按最多赞成票对它们进行排序。例如:

第 6 篇文章(点赞数:1) 帖子 5(点赞数:2) 帖子 4(点赞数:1)

我希望它们像这样排序:

第 5 篇文章(点赞数:2) 帖子 6(点赞数:1) 帖子 4(点赞数:1)

我对平局会发生什么不太感兴趣,但至少,我希望获得更多赞成票的帖子比那些获得更少赞成票的帖子列出得更高。

当然,我可以编写一个方法来对这些进行排序,但肯定有一种方法可以使用 MongoDB 来做到这一点。

下面是我尝试实现这种排序的一些方法。

// Use sort for date and then use it again for upvotes_count
Post.find()
    .sort({_id: -1})
    .sort({upvotes_count: -1})
    .limit(3)
    .exec( function(err, posts) {
        if (err) res.send(err);
        console.log(posts);
        res.json(posts);
     });

// Use sort for date, limit the results to three, and then
// use it again for upvotes_count
Post.find()
    .sort({_id: -1})
    .limit(3)
    .sort({upvotes_count: -1})
    .exec( function(err, posts) {
        if (err) res.send(err)
        console.log(posts);
        res.json(posts);
    });

// Use sort for date and upvotes_count in one step.
Post.find()
    .sort({_id: -1, upvotes_count: -1})
    .limit(3)
    .exec( function(err, posts) {
        if (err) res.send(err);
        console.log(posts);
        res.json(posts);
     });

没有一个有效。

最佳答案

引用sort()定义。

sort({_id: -1, upvotes_count: -1})

表示首先对 _id 进行排序,然后仅对那些相同 _id 帖子按 desc 顺序对 upvotes_count 进行排序。不幸的是,_idObjectId ,它是 12 字节 BSON 类型,构造方法为:

  • 一个 4 字节值,表示自 Unix 纪元以来的秒数,
  • 3 字节机器标识符,
  • 2 字节进程 ID,以及
  • 一个 3 字节计数器,以随机值开始。

很难获得相同的ObjectId。也就是说,每条记录的_id在该文档中应该是唯一的。因此,您的测试代码的结果只是按 _id desc 排序。

这是一个例子,

+---------+---------------+
| _id     |  upvote_count |
+---------+---------------+
|  1      |      5        |
|  4      |      7        |
|  3      |      9        |
|  4      |      8        |

sort({_id: -1, upvotes_count: -1}) 的结果应该是

+---------+---------------+
| _id     |  upvote_count |
+---------+---------------+
|  4      |      8        |
|  4      |      7        |
|  3      |      9        |
|  1      |      5        |

upvote_count 将根据相同的 _id 进行排序。

但是,在这种情况下。在这种情况下,有相同的 _id

+---------+---------------+
| _id     |  upvote_count |
+---------+---------------+
|  1      |      5        |
|  4      |      7        |
|  3      |      9        |
|  2      |      8        |

sort({_id: -1, upvotes_count: -1}) 的结果应该是

+---------+---------------+
| _id     |  upvote_count |
+---------+---------------+
|  1      |      5        |
|  2      |      8        |
|  3      |      9        |
|  4      |      7        |

关于node.js - 如何用 Mongoose 对两个字段进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35027471/

相关文章:

c++ - C++ 中的排序函数如何工作?

mysql - AWS 的 NodeJS 部署容量规划

mongodb - 如何检查 MongoDB 的当前配置

mongodb - 使用 --columnsHaveTypes 将数据从 csv 文件导入 Mongodb,指定特定字段为日期

java - 对字符串数组进行排序

c++ - 重载运算符 < 包含类对象

javascript - 测试 axios 请求的 header

node.js - 客户端是否可以向 websocket 服务器指定其类型?

node.js - Sequelize/Umzug : where to store a state of a database schema?

node.js - Mongoose 4 至 5 : connection. db.collection() 未定义