ChatGPT解决这个技术问题 Extra ChatGPT

How to sort in mongoose?

I find no doc for the sort modifier. The only insight is in the unit tests: spec.lib.query.js#L12

writer.limit(5).sort(['test', 1]).group('name')

But it doesn't work for me:

Post.find().sort(['updatedAt', 1]);
Read this answer for an up to day answer.

A
Akash Kumar Verma

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

    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) { ... });

This is almost a straight copy of the answer linked by Francisco Presencia. Unfortunately the highest voted answers are outdated and unnecessarily long.
This is not quite correct as of today. {sort: [['date', 1]]} will not work, but .sort([['date', -1]]) will work. See this answer: stackoverflow.com/a/15081087/404699
@steampowered thanks, i'll make an edit, you are very welcome to let me know or edit if I got it wrong.
Would love to see a few remarks on what the result is, esp. what the 1 and -1 values mean, instead of having 4 code lines without much context
2
2 revs

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

// 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
})

in mongoose 3 you can't use Array for field selection anymore - it has to be String or Object
btw, if you want all fields, you can just pull null in that section (at least in 3.8)
A
Akash Kumar Verma

As of Mongoose 3.8.x:

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


S
Syntle

UPDATE:

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

Try:

Post.find().sort([['updatedAt', 'descending']]).all((posts) => {
  // do something with the array of posts
});

In the latest Mongoose (2.4.10) it's .sort("updatedAt", -1).
In the even more latest Mongoose (3.5.6-pre, but I'm pretty sure it's valid for all of 3.x) it's .sort({updatedAt: -1}) or .sort('-updatedAt').
Then you should use exec(function (posts) {… instead of all
I've got a all() must be used after where() when called with these arguments in Mongoose 4.6.5...
I use this in Node.js like javascript Post.find().sort({updatedAt: -1}).all((posts) => {
S
Sultan Aslam

Mongoose v5.x.x

sort by ascending order

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

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


As @Sunny Sultan pointed portend out all Post.find({}, null, {sort: '-field'}, function(err, docs) { ... }); for descending and Post.find({}, null, {sort: 'field'}, function(err, docs) { ... }); for ascending work as well. In Fact as I pass the field and sort order in the http query parameters, combining them into a string and use this way to sort my bd query is my only option.
A
AJ.

Update

There is a better write up if this is confusing people; check out finding documents and how queries work in the mongoose manual. If you want to use the fluent api you can get a query object by not providing a callback to the find() method, otherwise you can specify the parameters as I outline below.

Original

Given a model object, per the docs on Model, this is how it can work for 2.4.1:

Post.find({search-spec}, [return field array], {options}, callback)

The search spec expects an object, but you can pass null or an empty object.

The second param is the field list as an array of strings, so you would supply ['field','field2'] or null.

The third param is the options as an object, which includes the ability to sort the result set. You would use { sort: { field: direction } } where field is the string fieldname test (in your case) and direction is a number where 1 is ascending and -1 is desceding.

The final param (callback) is the callback function which receives the collection of docs returned by the query.

The Model.find() implementation (at this version) does a sliding allocation of properties to handle optional params (which is what confused me!):

Model.find = function find (conditions, fields, options, callback) {
  if ('function' == typeof conditions) {
    callback = conditions;
    conditions = {};
    fields = null;
    options = null;
  } else if ('function' == typeof fields) {
    callback = fields;
    fields = null;
    options = null;
  } else if ('function' == typeof options) {
    callback = options;
    options = null;
  }

  var query = new Query(conditions, options).select(fields).bind(this, 'find');

  if ('undefined' === typeof callback)
    return query;

  this._applyNamedScope(query);
  return query.find(callback);
};

HTH


for projection : we need to provide string that contains column names seperated with space.
S
Salida Software

This is how I got sort to work in mongoose.js 2.0.4

var query = EmailModel.find({domain:"gmail.com"});
query.sort('priority', 1);
query.exec(function(error, docs){
  //...
});

佚名

Chaining with the query builder interface in Mongoose 4.

// Build up a query using chaining syntax. Since no callback is passed this will create an instance of Query.
var query = Person.
    find({ occupation: /host/ }).
    where('name.last').equals('Ghost'). // find each Person with a last name matching 'Ghost'
    where('age').gt(17).lt(66).
    where('likes').in(['vaporizing', 'talking']).
    limit(10).
    sort('-occupation'). // sort by occupation in decreasing order
    select('name occupation'); // selecting the `name` and `occupation` fields


// Excute the query at a later time.
query.exec(function (err, person) {
    if (err) return handleError(err);
    console.log('%s %s is a %s.', person.name.first, person.name.last, person.occupation) // Space Ghost is a talk show host
})

See the docs for more about queries.


R
Ramadhani Shemahonge

you can sort your query results by

Post.find().sort({createdAt: "descending"});


J
Jaskaran Singh
app.get('/getting',function(req,res){
    Blog.find({}).limit(4).skip(2).sort({age:-1}).then((resu)=>{
        res.send(resu);
        console.log(resu)
        // console.log(result)
    })
})

Output

[ { _id: 5c2eec3b8d6e5c20ed2f040e, name: 'e', age: 5, __v: 0 },
  { _id: 5c2eec0c8d6e5c20ed2f040d, name: 'd', age: 4, __v: 0 },
  { _id: 5c2eec048d6e5c20ed2f040c, name: 'c', age: 3, __v: 0 },
  { _id: 5c2eebf48d6e5c20ed2f040b, name: 'b', age: 2, __v: 0 } ]

P
Philipp Kyeck

with the current version of mongoose (1.6.0) if you only want to sort by one column, you have to drop the array and pass the object directly to the sort() function:

Content.find().sort('created', 'descending').execFind( ... );

took me some time, to get this right :(


M
Mark Nguyen

This is how I managed to sort and populate:

Model.find()
.sort('date', -1)
.populate('authors')
.exec(function(err, docs) {
    // code here
})

c
chovy
Post.find().sort({updatedAt: 1});

F
Frederick Haug

As of October 2020, to fix your issue you should add .exec() to the call. don't forget that if you want to use this data outside of the call you should run something like this inside of an async function.

let post = await callQuery();

async function callQuery() {
      return Post.find().sort(['updatedAt', 1].exec();
}

F
FrederickCook

Others worked for me, but this did:

  Tag.find().sort('name', 1).run(onComplete);

g
gtsouk
Post.find().sort({updatedAt:1}).exec(function (err, posts){
...
});

S
Sunny Sultan

Starting from 4.x the sort methods have been changed. If you are using >4.x. Try using any of the following.

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

T
Thavaprakash Swaminathan

This is what i did, it works fine.

User.find({name:'Thava'}, null, {sort: { name : 1 }})

H
Huỳnh Tấn Lực
Post.find().sort('updatedAt').exec((err, post) => {...});

Reference in here: https://mongoosejs.com/docs/queries.html


Hi and welcome to the community. While your answer may provide a solution, a good answer requires some explanation. Kindly add some references and a proper explanation.
M
Marzieh Mousavi

you can also use aggregate() for sorting

 const sortBy = req.params.sort;
  const limitNum = req.params.limit;
  const posts = await Post.aggregate([
    { $unset: ['field-1', 'field-2', 'field-3', 'field-4'] },
    { $match: { field-1: value} },
    { $sort: { [sortBy]: -1 } },  //-------------------> sort the result
    { $limit: Number(limitNum) },
  ]);