简体   繁体   English

在node.js中按日期排序mongodb集合

[英]sort mongodb collection by date in node.js

I'm new to mongodb and trying to figure out sorting with MongoClient in a node.js/express application. 我是mongodb的新手,试图在node.js / express应用程序中使用MongoClient进行排序。

This works in the mongo command line client: 这在mongo命令行客户端中有效:

db.mycollection.find().sort({"date":-1}); // displays by date, newest to oldest

I'm trying to achieve the same thing in my application: 我正在尝试在我的应用程序中实现相同的目的:

db.collection('mycollection').find().sort({"date":-1}); //order remains the same

How can I achieve the same result as the first query? 如何获得与第一个查询相同的结果? Thank you. 谢谢。

So, first, I'd recommend using Mongoose. 所以,首先,我建议使用猫鼬。 Failing that, however, the Node MongoClient puts things like sorting into the find() arguments, like so: 但是,如果失败,Node MongoClient会将诸如排序之类的内容放入find()参数中,如下所示:

db.collection('mycollection').find({}, {sort:{date:-1}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM