Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose find sort

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

sort mongoose response

Blah.find({}).sort({date: -1}).execFind(function(err,docs){
//code
});

-1 for descending & 1 for ascending
Comment

mongoose sort

  let limit = parseInt(req.body.limit) || 10
  let page = parseInt(req.body.page) - 1 || 0
  var query = {};
  let users = await Users.find({}).sort({ createdAt: -1 }).skip(limit * page).limit(limit).select("fields you want to select separated by space")
  const count = await Users.countDocuments(query);
  let obj = {
    Users: users,
    total: count,
    limit: limit,
    page: page + 1
  }
Comment

Mongoose sort

// sort by "field" ascending and "test" descending
query.sort({ field: 'asc', test: -1 });

// equivalent
query.sort('field -test');
Comment

PREVIOUS NEXT
Code Example
Javascript :: express js url with id 
Javascript :: js create array from for loop 
Javascript :: Random Integer 1-10 
Javascript :: onchange input jquery 
Javascript :: how to get the div value in jquery 
Javascript :: find input by value attribute javascript 
Javascript :: electron send message from renderer to main 
Javascript :: findbyid mongoose 
Javascript :: email regex pattern input css 
Javascript :: how to move div using jquery 
Javascript :: how to get checked value of checkbox in jquery 
Javascript :: split 2 arrays javascript 
Javascript :: how to remove class in all siblings javascript 
Javascript :: angular disable click 
Javascript :: forin js 
Javascript :: javascript set time to start of day 12 am 
Javascript :: javascript fire keypress event 
Javascript :: document.append 
Javascript :: Shortest ajax get method jquery 
Javascript :: javascript merge array 
Javascript :: create javascript array 
Javascript :: js sum of int in array 
Javascript :: angularjs datetime 
Javascript :: js assignment operators 
Javascript :: get value for radio button in jquery label 
Javascript :: click a link with javascript 
Javascript :: javascript key event 
Javascript :: checkbox jquery checked 
Javascript :: ref focus not working vue js 
Javascript :: CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =