node.js 如何在 MongoDB 中按日期对集合进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13847766/
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
How to sort a collection by date in MongoDB?
提问by flow
I am using MongoDB with Node.JS. I have a collection which contains a date and other rows. The date is a JavaScript Dateobject.
我在 Node.JS 中使用 MongoDB。我有一个包含日期和其他行的集合。日期是一个 JavaScriptDate对象。
How can I sort this collection by date?
如何按日期对这个集合进行排序?
回答by Sushant Gupta
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).
在许多用例中,我们希望返回最新的记录(例如最新的更新/插入)。
回答by JohnnyHK
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 datefieldusing any of the following ways:
针对 1.4.28 node.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'或者'ascending'也可以用来代替1.
To sort descending, use 'desc', 'descending', or -1in place of the 1.
要降序排序,请使用'desc'、'descending'或-1代替1。
回答by Rich Rajah
db.getCollection('').find({}).sort({_id:-1})
This will sort your collection in descending order based on the date of insertion
这将根据插入日期按降序对您的集合进行排序
回答by krikara
Sushant Gupta's answers are a tad bit outdated and don't work anymore.
Sushant Gupta 的答案有点过时,不再适用。
The following snippet should be like this now :
下面的代码片段现在应该是这样的:
collection.find({}, {"sort" : ['datefield', 'asc']} ).toArray(function(err,docs) {});
collection.find({}, {"sort" : ['datefield', 'asc']} ).toArray(function(err,docs) {});
回答by GoldfishGrenade
This worked for me:
这对我有用:
collection.find({}, {"sort" : [['datefield', 'asc']]}, function (err, docs) { ... });
Using Node.js, Express.js, and Monk
使用 Node.js、Express.js 和 Monk
回答by Mendon Ashwini
collection.find().sort('date':1).exec(function(err, doc) {});
this worked for me
这对我有用
referred https://docs.mongodb.org/getting-started/node/query/
回答by emil.c
With mongoose it's as simple as:
使用猫鼬,它很简单:
collection.find().sort('-date').exec(function(err, collectionItems) {
// here's your code
})
回答by DB Prasad
Additional Square [ ]Bracket is required for sorting parameter to work.
需要额外的 Square [ ]括号才能对参数进行排序才能工作。
collection.find({}, {"sort" : [['datefield', 'asc']]} ).toArray(function(err,docs) {});
回答by Amine_Dev
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-14T08:14:00.201Z")
now just use the query like this :
现在只需使用这样的查询:
collection.find( { query : query ,$orderby :{start_date : -1}} ,function (err, cursor) {...}
that's it :)
就是这样 :)
回答by Pransh Tiwari
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).
使用猫鼬时,我无法使用“toArray”,并且出现错误:TypeError: Collection.find(...).sort(...).toArray is not a function.本地 MongoDB NodeJS 驱动程序(参考)的 Cursor 类中存在 toArray 函数。
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
})

