本文介绍了排序优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoDB数据库中有一个集合Answers,我使用每个答案具有的投票数对文档进行排序.

I have a collection Answers in a MongoDB database and I sort the documents with the number of upvotes each answer has.

Answers.find({}, {sort: {voteCount: -1}})

但是,其中一些答案是由教师发布的,应该在常规"答案之前对其进行排序.讲师发布的答案的字段为isInstructor: true.

Some of these answers, however, are posted by instructors and they should be sorted before of "regular" answers. Answer posted by instructors have a field isInstructor: true.How do I retrieve my list of answers sorted in a way that instructors' answers come first (sorted by voteCounts as well) and then, after those, the normal answers (still sorted by voteCounts)?

推荐答案

好如我的评论中所述,您必须按isInstructor降序对answers进行排序,并按voteCount降序对answers进行排序.另请注意, .find 的第二个参数> 是投影文档.因此,要对文档进行排序,您需要使用 方法.

Well As mention in my comment you must sort your answers by isInstructor in descending order and by voteCount in descending order. Also note that the second argument to .find is a projection document. Thus to sort your document you need to use the cursor.sort method instead.

Answers.find().sort({ "isInstructor": -1, "voteCount": -1 })

这篇关于排序优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 20:16