I am using MongoDB with Node.JS. I have a collection which contains a date and other rows. The date is a JavaScript Date
object.
我正在使用MongoDB和Node.JS。我有一个包含日期和其他行的集合。日期是一个JavaScript日期对象。
How can I sort this collection by date?
我如何按日期对这些收藏品进行分类?
10 个解决方案
#1
109
Just a slight modification to @JohnnyHK answer
对@JohnnyHK的回答做一个小小的修改
collection.find().sort({datefield: -1}, function(err, cursor){...});
In many use cases we wish to have latest records to be returned (like for latest updates / inserts).
在许多用例中,我们希望返回最新的记录(比如最新的更新/插入)。
#2
31
Sorting by date doesn't require anything special. Just sort by the desired date field of the collection.
按日期排序不需要任何特殊的东西。只需按集合的所需日期字段进行排序。
Updated for the 1.4.28 node.js native driver, you can sort ascending on datefield
using any of the following ways:
更新了1.4.28节点。js本地驱动程序,您可以使用以下任何一种方式在datefield上进行升序排序:
collection.find().sort({datefield: 1}).toArray(function(err, docs) {...});
collection.find().sort('datefield', 1).toArray(function(err, docs) {...});
collection.find().sort([['datefield', 1]]).toArray(function(err, docs) {...});
collection.find({}, {sort: {datefield: 1}}).toArray(function(err, docs) {...});
collection.find({}, {sort: [['datefield', 1]]}).toArray(function(err, docs) {...});
'asc'
or 'ascending'
can also be used in place of the 1
.
“asc”或“升序”也可以用来代替“1”。
To sort descending, use 'desc'
, 'descending'
, or -1
in place of the 1
.
要对降序排序,使用'desc'、'降序'或-1代替1。
#3
15
Sushant Gupta's answers are a tad bit outdated and don't work anymore.
苏尚特·古普塔的答案有点过时,再也行不通了。
The following snippet should be like this now :
下面的片段应该是这样的:
collection.find({}, {"sort" : ['datefield', 'asc']} ).toArray(function(err,docs) {});
收集。找到({ },{“排序”:[‘datefield’,‘asc]}).toArray(函数(呃,文档){ });
#4
12
This worked for me:
这工作对我来说:
collection.find({}, {"sort" : [['datefield', 'asc']]}, function (err, docs) { ... });
Using Node.js, Express.js, and Monk
使用节点。js,表达。js,和尚
#5
6
collection.find().sort('date':1).exec(function(err, doc) {});
this worked for me
这为我工作
referred https://docs.mongodb.org/getting-started/node/query/
引用https://docs.mongodb.org/getting-started/node/query/
#6
6
db.getCollection('').find({}).sort({_id:-1})
This will sort your collection in descending order based on the date of insertion
这将根据插入日期按降序对集合进行排序
#7
5
With mongoose it's as simple as:
猫鼬很简单:
collection.find().sort('-date').exec(function(err, collectionItems) {
// here's your code
})
#8
4
Additional Square [ ] Bracket is required for sorting parameter to work.
排序参数的工作需要附加方括号[]。
collection.find({}, {"sort" : [['datefield', 'asc']]} ).toArray(function(err,docs) {});
#9
2
if your date format is like this : 14/02/1989 ----> you may find some problems
如果您的日期格式是:14/02/1989—>,您可能会发现一些问题
you need to use ISOdate like this :
你需要像这样使用ISOdate:
var start_date = new Date(2012, 07, x, x, x);
-----> the result ------>ISODate("2012-07-14T08:14:00.201Z")
- - - - - - >结果- - - - - - > ISODate(“2012 - 07 - 14 t08:14:00.201z”)
now just use the query like this :
现在只需使用如下查询:
collection.find( { query : query ,$orderby :{start_date : -1}} ,function (err, cursor) {...}
that's it :)
就是这样:)
#10
0
With mongoose I was not able to use 'toArray', and was getting the error: TypeError: Collection.find(...).sort(...).toArray is not a function.
The toArray function exists on the Cursor class from the Native MongoDB NodeJS driver (reference).
对于mongoose,我不能使用“toArray”,并得到了错误:TypeError: Collection.find(…).sort(…)。toArray不是函数。toArray函数从本地MongoDB NodeJS驱动程序(引用)上存在于游标类上。
Also sort accepts only one parameter, so you can't pass your function inside it.
sort只接受一个参数,所以不能在其中传递函数。
This worked for me (as answered by Emil):
这对我起了作用(埃米尔的回答):
collection.find().sort('-date').exec(function(error, result) {
// Your code
})
#1
109
Just a slight modification to @JohnnyHK answer
对@JohnnyHK的回答做一个小小的修改
collection.find().sort({datefield: -1}, function(err, cursor){...});
In many use cases we wish to have latest records to be returned (like for latest updates / inserts).
在许多用例中,我们希望返回最新的记录(比如最新的更新/插入)。
#2
31
Sorting by date doesn't require anything special. Just sort by the desired date field of the collection.
按日期排序不需要任何特殊的东西。只需按集合的所需日期字段进行排序。
Updated for the 1.4.28 node.js native driver, you can sort ascending on datefield
using any of the following ways:
更新了1.4.28节点。js本地驱动程序,您可以使用以下任何一种方式在datefield上进行升序排序:
collection.find().sort({datefield: 1}).toArray(function(err, docs) {...});
collection.find().sort('datefield', 1).toArray(function(err, docs) {...});
collection.find().sort([['datefield', 1]]).toArray(function(err, docs) {...});
collection.find({}, {sort: {datefield: 1}}).toArray(function(err, docs) {...});
collection.find({}, {sort: [['datefield', 1]]}).toArray(function(err, docs) {...});
'asc'
or 'ascending'
can also be used in place of the 1
.
“asc”或“升序”也可以用来代替“1”。
To sort descending, use 'desc'
, 'descending'
, or -1
in place of the 1
.
要对降序排序,使用'desc'、'降序'或-1代替1。
#3
15
Sushant Gupta's answers are a tad bit outdated and don't work anymore.
苏尚特·古普塔的答案有点过时,再也行不通了。
The following snippet should be like this now :
下面的片段应该是这样的:
collection.find({}, {"sort" : ['datefield', 'asc']} ).toArray(function(err,docs) {});
收集。找到({ },{“排序”:[‘datefield’,‘asc]}).toArray(函数(呃,文档){ });
#4
12
This worked for me:
这工作对我来说:
collection.find({}, {"sort" : [['datefield', 'asc']]}, function (err, docs) { ... });
Using Node.js, Express.js, and Monk
使用节点。js,表达。js,和尚
#5
6
collection.find().sort('date':1).exec(function(err, doc) {});
this worked for me
这为我工作
referred https://docs.mongodb.org/getting-started/node/query/
引用https://docs.mongodb.org/getting-started/node/query/
#6
6
db.getCollection('').find({}).sort({_id:-1})
This will sort your collection in descending order based on the date of insertion
这将根据插入日期按降序对集合进行排序
#7
5
With mongoose it's as simple as:
猫鼬很简单:
collection.find().sort('-date').exec(function(err, collectionItems) {
// here's your code
})
#8
4
Additional Square [ ] Bracket is required for sorting parameter to work.
排序参数的工作需要附加方括号[]。
collection.find({}, {"sort" : [['datefield', 'asc']]} ).toArray(function(err,docs) {});
#9
2
if your date format is like this : 14/02/1989 ----> you may find some problems
如果您的日期格式是:14/02/1989—>,您可能会发现一些问题
you need to use ISOdate like this :
你需要像这样使用ISOdate:
var start_date = new Date(2012, 07, x, x, x);
-----> the result ------>ISODate("2012-07-14T08:14:00.201Z")
- - - - - - >结果- - - - - - > ISODate(“2012 - 07 - 14 t08:14:00.201z”)
now just use the query like this :
现在只需使用如下查询:
collection.find( { query : query ,$orderby :{start_date : -1}} ,function (err, cursor) {...}
that's it :)
就是这样:)
#10
0
With mongoose I was not able to use 'toArray', and was getting the error: TypeError: Collection.find(...).sort(...).toArray is not a function.
The toArray function exists on the Cursor class from the Native MongoDB NodeJS driver (reference).
对于mongoose,我不能使用“toArray”,并得到了错误:TypeError: Collection.find(…).sort(…)。toArray不是函数。toArray函数从本地MongoDB NodeJS驱动程序(引用)上存在于游标类上。
Also sort accepts only one parameter, so you can't pass your function inside it.
sort只接受一个参数,所以不能在其中传递函数。
This worked for me (as answered by Emil):
这对我起了作用(埃米尔的回答):
collection.find().sort('-date').exec(function(error, result) {
// Your code
})