Reputation: 37
the code like that:
Task.findByIdAndUpdate({_id: req.params.task_id}, updateObj, {new: true}, function (err, updatedTasks) {
if (err) {
return resultCode.serverError(req, res)
}
resultCode.success(req, res, updatedTasks);
});
I want to sort the list 'updateTasks' by the field 'create_at', how can i do?
Upvotes: 1
Views: 1095
Reputation: 2711
Sorting in Mongoose has evolved over the releases. As of the 4.7.x release of Mongoose, a descending sort on the date field can be done in any of the following ways:
Task.findByIdAndUpdate({ ... }).sort('-create_at').exec(function(err, updatedTasks) { ... });
Task.findByIdAndUpdate({ ... }).sort({create_at: -1}).exec(function(err, updatedTasks) { ... });
Task.findByIdAndUpdate({ ... }).sort({create_at: 'desc'}).exec(function(err, updatedTasks) { ... });
Task.findByIdAndUpdate({ ... }).sort({create_at: 'descending'}).exec(function(err, updatedTasks) { ... });
Task.findByIdAndUpdate({ ... }).sort([['create_at', -1]]).exec(function(err, updatedTasks) { ... });
For an ascending sort, omit the -
prefix on the string version or use values of 1
, asc
, or ascending
.
Upvotes: 3