node.js 在猫鼬中,我如何按日期排序?(节点.js)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5825520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Mongoose, how do I sort by date? (node.js)
提问by TIMEX
let's say I run this query in Mongoose:
假设我在 Mongoose 中运行此查询:
Room.find({}, function(err,docs){
}).sort({date:-1});
This doesn't work!
这不起作用!
回答by JohnnyHK
Sortingin Mongoose has evolved over the releases such that some of these answers are no longer valid. As of the 4.1.xrelease of Mongoose, a descending sort on the datefield can be done in any of the following ways:
Mongoose 中的排序随着版本的不断发展而发展,因此其中一些答案不再有效。从Mongoose的4.1.x版本开始,date可以通过以下任何一种方式对字段进行降序排序:
Room.find({}).sort('-date').exec(function(err, docs) { ... });
Room.find({}).sort({date: -1}).exec(function(err, docs) { ... });
Room.find({}).sort({date: 'desc'}).exec(function(err, docs) { ... });
Room.find({}).sort({date: 'descending'}).exec(function(err, docs) { ... });
Room.find({}).sort([['date', -1]]).exec(function(err, docs) { ... });
Room.find({}, null, {sort: '-date'}, function(err, docs) { ... });
Room.find({}, null, {sort: {date: -1}}, function(err, docs) { ... });
For an ascending sort, omit the -prefix on the string version or use values of 1, asc, or ascending.
对于升序排序,省略了-对字符串版本或使用值的前缀1,asc或ascending。
回答by TIMEX
The correct answer is:
正确答案是:
Blah.find({}).sort({date: -1}).execFind(function(err,docs){
});
回答by Jimmy Hillis
Been dealing with this issue today using Mongoose 3.5(.2) and none of the answers quite helped me solve this issue. The following code snippet does the trick
今天使用 Mongoose 3.5(.2) 处理这个问题,但没有一个答案能帮助我解决这个问题。以下代码片段可以解决问题
Post.find().sort('-posted').find(function (err, posts) {
// user posts array
});
You can send any standard parameters you need to find()(e.g. where clauses and return fields) but nocallback. Without a callback it returns a Query object which you chain sort()on. You need to call find()again (with or without more parameters -- shouldn't need any for efficiency reasons) which will allow you to get the result set in your callback.
您可以发送您需要的任何标准参数find()(例如 where 子句和返回字段),但不能发送回调。如果没有回调,它会返回一个您链接的 Query 对象sort()。您需要find()再次调用(使用或不使用更多参数 - 出于效率原因不应该需要任何参数),这将允许您在回调中获得结果集。
回答by lynx_vbg
Post.find().sort({date:-1}, function(err, posts){
});
Should work as well
也应该工作
EDIT:
编辑:
You can also try using this if you get the error sort() only takes 1 Argument:
如果出现错误,您也可以尝试使用它sort() only takes 1 Argument:
Post.find({}, {
'_id': 0, // select keys to return here
}, {sort: '-date'}, function(err, posts) {
// use it here
});
回答by Noah
I do this:
我这样做:
Data.find( { $query: { user: req.user }, $orderby: { dateAdded: -1 } } function ( results ) {
...
})
This will show the most recent things first.
这将首先显示最近的事情。
回答by davidsonsns
Short solution:
简短的解决方案:
const query = {}
const projection = {}
const options = { sort: { id: 1 }, limit: 2, skip: 10 }
Room.find(query, projection, options).exec(function(err, docs) { ... });
回答by neebz
See if this helps > How to sort in mongoose?
看看这是否有帮助 >如何在猫鼬中排序?
Also read this > http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order
另请阅读此 > http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order
回答by Mike K
You can also sort by the _idfield. For example, to get the most recent record, you can do,
您还可以按_id字段排序。例如,要获取最近的记录,您可以这样做,
const mostRecentRecord = await db.collection.findOne().sort({ _id: -1 });
It's much quicker too, because I'm more than willing to bet that your datefield is not indexed.
它也快得多,因为我非常愿意打赌你的date领域没有被索引。

