mongodb 如何使用mongoose对两个字段进行排序?

cbjzeqam  于 11个月前  发布在  Go
关注(0)|答案(3)|浏览(145)

我试图让用户看到热门帖子。一般的想法是按最近的帖子排序(_id:-1),然后按最多的投票排序(upvotes_count:-1),然后限制结果(.limit(3))。这有点简化,所以请忽略“热门帖子”的这种实现。
不幸的是,我不能以我想要的方式返回两个排序。所以对于六个帖子的集合,它返回最近的三个,但它不会按照最多的赞成票对它们进行排序。例如:
第6篇(upvotes:1)第5篇(upvotes:2)第4篇(upvotes:1)
我希望它们像这样排序:
第5篇(upvotes:2)第6篇(upvotes:1)第4篇(upvotes: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);
     });

字符串
没有一个奏效。

vpfxa7rd

vpfxa7rd1#

参见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        |

s71maibg

s71maibg2#

你可以找到大多数投票的文件在某些时候

在过去24小时内投票最多

var yesterday = Date.now()- 1000*60*60*24;

// assuming created_at contains time-stamp
find({created_at:{$gt:yesterday}}).sort({upvotes_count: -1}).limit(3)

字符串

gkl3eglg

gkl3eglg3#

exports.getPosts = function(number, callback) { 
  Post.find().sort({ upvotes_count: -1 }).select({ _id: 1 })
    .limit(number)
    .exec(
      function(err, projects) {
        callback(null, projects);
      }
    );
};

字符串

相关问题