在蒙古语中,如何按日期排序?(node . js)

时间:2021-02-02 02:36:41

let's say I run this query in Mongoose:

假设我用Mongoose运行这个查询:

Room.find({}, function(err,docs){

}).sort({date:-1}); 

This doesn't work!

这并不工作!

7 个解决方案

#1


310  

Sorting in Mongoose has evolved over the releases such that some of these answers are no longer valid. As of the 4.1.x release of Mongoose, a descending sort on the date field can be done in any of the following ways:

Mongoose中的排序在发行版中已经有所发展,因此有些答案不再有效。4.1的。Mongoose的x版本在日期字段上的降序排序可以通过以下任何一种方式完成:

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或升序值。

#2


48  

The correct answer is:

正确的答案是:

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

});

#3


10  

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 no callback. 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.

您可以发送任何您需要查找()的标准参数(例如where子句和返回字段),但不发送回调。没有回调,它返回一个查询对象,您可以对其进行链排序()。您需要再次调用find()(带或不带更多参数——出于效率原因不需要任何参数),这将允许您在回调中获得结果集。

#4


3  

I do this:

我这样做:

Data.find( { $query: { user: req.user }, $orderby: { dateAdded: -1 } } function ( results ) {
    ...
})

This will show the most recent things first.

这将首先显示最近发生的事情。

#5


2  

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 :

如果您得到error sort()只接受1个参数,也可以尝试使用这个参数:

Post.find({}, {
    '_id': 0,    // select keys to return here
}, {sort: '-date'}, function(err, posts) {
    // use it here
});

#6


1  

See if this helps > How to sort in mongoose?

看看这是否有助于>如何在mongoose中排序?

Also read this > http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order

也可以阅读这个> http://www.mongodb.org/display/docs/sort +和+Natural+Order

#7


0  

Short solution:

短期解决方案:

const query = {}
const projection = {}
const options = { sort: { id: 1 }, limit: 2, skip: 10 }

Room.find(query, projection, options).exec(function(err, docs) { ... });

#1


310  

Sorting in Mongoose has evolved over the releases such that some of these answers are no longer valid. As of the 4.1.x release of Mongoose, a descending sort on the date field can be done in any of the following ways:

Mongoose中的排序在发行版中已经有所发展,因此有些答案不再有效。4.1的。Mongoose的x版本在日期字段上的降序排序可以通过以下任何一种方式完成:

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或升序值。

#2


48  

The correct answer is:

正确的答案是:

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

});

#3


10  

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 no callback. 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.

您可以发送任何您需要查找()的标准参数(例如where子句和返回字段),但不发送回调。没有回调,它返回一个查询对象,您可以对其进行链排序()。您需要再次调用find()(带或不带更多参数——出于效率原因不需要任何参数),这将允许您在回调中获得结果集。

#4


3  

I do this:

我这样做:

Data.find( { $query: { user: req.user }, $orderby: { dateAdded: -1 } } function ( results ) {
    ...
})

This will show the most recent things first.

这将首先显示最近发生的事情。

#5


2  

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 :

如果您得到error sort()只接受1个参数,也可以尝试使用这个参数:

Post.find({}, {
    '_id': 0,    // select keys to return here
}, {sort: '-date'}, function(err, posts) {
    // use it here
});

#6


1  

See if this helps > How to sort in mongoose?

看看这是否有助于>如何在mongoose中排序?

Also read this > http://www.mongodb.org/display/DOCS/Sorting+and+Natural+Order

也可以阅读这个> http://www.mongodb.org/display/docs/sort +和+Natural+Order

#7


0  

Short solution:

短期解决方案:

const query = {}
const projection = {}
const options = { sort: { id: 1 }, limit: 2, skip: 10 }

Room.find(query, projection, options).exec(function(err, docs) { ... });