如何在MongoDB中按日期对集合进行排序? [英] How to sort a collection by date in MongoDB?

查看:51
本文介绍了如何在MongoDB中按日期对集合进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将MongoDB与Node.JS结合使用.我有一个包含日期和其他行的集合.日期是一个JavaScript Date对象.

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.

如何按日期对集合进行排序?

How can I sort this collection by date?

推荐答案

只需对@JohnnyHK答案进行一些修改

Just a slight modification to @JohnnyHK answer

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).

这篇关于如何在MongoDB中按日期对集合进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆