乐闻世界logo
搜索文章和话题

How to sort in mongoose

5 个月前提问
3 个月前修改
浏览次数56

6个答案

1
2
3
4
5
6

在Mongoose中,排序可以通过在查询后使用 .sort() 方法来实现。该方法接受一个参数,该参数可以是字符串或对象,用于指定根据哪些字段进行排序以及排序的方向(升序或降序)。在MongoDB中,升序使用 1 表示,而降序使用 -1 表示。

以下是如何使用 .sort() 方法的一些例子:

使用字符串进行排序

如果您只需要根据一个字段进行排序,可以直接传递一个字段名,前面带有 - 表示降序,不带任何符号表示升序。

javascript
// 升序排序 MyModel.find().sort('field').exec((err, docs) => { // 处理结果或错误 }); // 降序排序 MyModel.find().sort('-field').exec((err, docs) => { // 处理结果或错误 });

使用对象进行排序

如果您要根据多个字段进行排序,可以传递一个对象,键是字段名,值是 1-1 来分别表示升序或降序。

javascript
// 根据多个字段进行排序 MyModel.find().sort({ field1: 1, field2: -1 }).exec((err, docs) => { // 处理结果或错误 });

示例

假设您有一个用户模型 User,并且您想根据年龄进行升序排序,然后根据用户名进行降序排序,您可以这样做:

javascript
User.find().sort({ age: 1, username: -1 }).exec((err, users) => { if (err) { console.error(err); } else { console.log(users); // 这将输出根据年龄升序、用户名降序排序的用户数组 } });

通过这种方式,您可以轻松地根据一个或多个字段对查询结果进行排序,这在处理大量数据时尤其有用,可以帮助您更快地定位到感兴趣的记录。

2024年6月29日 12:07 回复

In Mongoose, a sort can be done in any of the following ways:

shell
Post.find({}).sort('test').exec(function(err, docs) { ... }); Post.find({}).sort([['date', -1]]).exec(function(err, docs) { ... }); Post.find({}).sort({test: 1}).exec(function(err, docs) { ... }); Post.find({}, null, {sort: {date: 1}}, function(err, docs) { ... });
2024年6月29日 12:07 回复

This is how I got sort to work in mongoose 2.3.0 :)

shell
// Find First 10 News Items News.find({ deal_id:deal._id // Search Filters }, ['type','date_added'], // Columns to Return { skip:0, // Starting Row limit:10, // Ending Row sort:{ date_added: -1 //Sort by Date Added DESC } }, function(err,allNews){ socket.emit('news-load', allNews); // Do something with the array of 10 objects })
2024年6月29日 12:07 回复

As of Mongoose 3.8.x:

shell
model.find({ ... }).sort({ field : criteria}).exec(function(err, model){ ... });

Where:

criteria can be asc, desc, ascending, descending, 1, or -1

Note: Use quotation marks or double quote

use "asc", "desc", "ascending", "descending", 1, or -1

2024年6月29日 12:07 回复

UPDATE:

shell
Post.find().sort({'updatedAt': -1}).all((posts) => { // do something with the array of posts });

Try:

shell
Post.find().sort([['updatedAt', 'descending']]).all((posts) => { // do something with the array of posts });
2024年6月29日 12:07 回复

Mongoose v5.x.x

sort by ascending order

shell
Post.find({}).sort('field').exec(function(err, docs) { ... }); Post.find({}).sort({ field: 'asc' }).exec(function(err, docs) { ... }); Post.find({}).sort({ field: 'ascending' }).exec(function(err, docs) { ... }); Post.find({}).sort({ field: 1 }).exec(function(err, docs) { ... }); Post.find({}, null, {sort: { field : 'asc' }}), function(err, docs) { ... }); Post.find({}, null, {sort: { field : 'ascending' }}), function(err, docs) { ... }); Post.find({}, null, {sort: { field : 1 }}), function(err, docs) { ... });

sort by descending order

shell
Post.find({}).sort('-field').exec(function(err, docs) { ... }); Post.find({}).sort({ field: 'desc' }).exec(function(err, docs) { ... }); Post.find({}).sort({ field: 'descending' }).exec(function(err, docs) { ... }); Post.find({}).sort({ field: -1 }).exec(function(err, docs) { ... }); Post.find({}, null, {sort: { field : 'desc' }}), function(err, docs) { ... }); Post.find({}, null, {sort: { field : 'descending' }}), function(err, docs) { ... }); Post.find({}, null, {sort: { field : -1 }}), function(err, docs) { ... });

For Details: https://mongoosejs.com/docs/api.html#query_Query-sort

2024年6月29日 12:07 回复

你的答案