Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

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]);

2 Answers

0 votes
by (106k points)

You can use the below-mentioned code to sort in MongoDB :-

News.find({ 

deal_id:deal._id

}, 

['type','date_added'], 

skip:0,

limit:10, 

sort:{ 

date_added: -1 

}, 

function(err,allNews){ 

socket.emit('news-load', allNews); 

})

0 votes
by (107k points)

For Mongoose 3.8.x, sorting can be done with the help of the following syntax:

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

Where x can be either:

  1. asc

  2. desc 

  3. ascending

  4. descending 

  5. 1

  6. -1

Related questions

0 votes
1 answer
+1 vote
2 answers
0 votes
2 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...