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) { ... });
Blah.find({}).sort({date: -1}).execFind(function(err,docs){
//code
});
-1 for descending & 1 for ascending
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
}
// sort by "field" ascending and "test" descending
query.sort({ field: 'asc', test: -1 });
// equivalent
query.sort('field -test');