如何用猫鼬对两个字段进行排序? [英] How to sort two fields with mongoose?
问题描述
我正试图允许用户查看热门帖子.总体思路是,按最新帖子(_id:-1)排序,然后按最新投票(upvotes_count:-1)排序,然后限制结果(.limit(3)).这有点简化,所以请忽略这种趋势文章"的实现.
I'm trying to allow users to see trending posts. The general idea is to sort by the most recent posts (_id: -1) and then sort those by most upvotes (upvotes_count: -1) and then limiting the results (.limit(3)). This is a bit simplified, so please ignore this implementation of "trending posts".
不幸的是,我无法以我想要的方式返回两种.因此,在收集了六个帖子之后,它会返回最近的三个帖子,但是并不会按照大多数投票对它们进行排序.例如:
Unfortunately, I'm not able to return two sorts in the way that I want. So with a collection of six posts, it returns the most recent three, but it doesn't then sort them by most upvotes. For instance:
第6个帖子(投票数:1) 第5个帖子(投票数:2) 第4个帖子(投票数:1)
Post 6 (upvotes: 1) Post 5 (upvotes: 2) Post 4 (upvotes: 1)
我希望对它们进行排序:
I want them to be sorted like so:
第5个帖子(投票数:2) 第6个帖子(投票数:1) 第4个帖子(投票数:1)
Post 5 (upvotes: 2) Post 6 (upvotes: 1) Post 4 (upvotes: 1)
我对平局中发生的事情不那么感兴趣,但是至少,我希望将具有较高投票权的帖子列出来高于具有较少投票权的帖子.
I'm not so interested in what happens with ties, but at a minimum, I want the posts that have more upvotes to be listed higher than those with less upvotes.
当然,我可以编写一种方法来对这些内容进行排序,但是肯定有一种方法可以对MongoDB进行处理.
Of course, I could write a method to sort these, but surely there is a way to do this with MongoDB.
下面是我尝试实现这种方式的一些方法.
Below are some of the ways I've tried to implement this sort.
// 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()
定义.
Refer to sort()
definition.
sort({_id: -1, upvotes_count: -1})
表示首先对_id
进行排序,然后仅对那些相同 _id
帖子按降序对upvotes_count
进行排序.不幸的是,_id
是 ObjectId
,它是12个字节BSON类型,使用以下方式构造:
means sort the _id
firstly, then sort upvotes_count
by desc order only for those same _id
posts. Unfortunately, the _id
is ObjectId
, which is 12-byte BSON type, constructed using:
- 一个4字节的值,表示自Unix时代以来的秒数,
- 3字节机器标识符,
- 2字节的进程ID,和
- 一个3字节计数器,以随机值开头.
很难获得相同的ObjectId
.即,每个记录的_id
在此文档中应该是唯一的.结果,测试代码的结果只是按_id
desc排序.
It is hard to get the same ObjectId
. Namely, the _id
of every record should be unique in this document. As a result, the result of your test codes are just ordered by _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
进行排序.
The upvote_count
would be sorted for same _id
.
但是,在这种情况下.在这种情况下,在同一_id
上.
However, in this case. There is on same _id
in this case.
+---------+---------------+
| _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 |
这篇关于如何用猫鼬对两个字段进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!