获取mongodb集合的最新记录 [英] Get the latest record from mongodb collection

查看:62
本文介绍了获取mongodb集合的最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道集合中的最新记录.该怎么做?

I want to know the most recent record in a collection. How to do that?

注意:我知道以下命令行查询有效:

Note: I know the following command line queries works:

1. db.test.find().sort({"idate":-1}).limit(1).forEach(printjson);
2. db.test.find().skip(db.test.count()-1).forEach(printjson)

在idate处添加了时间戳.

where idate has the timestamp added.

问题是收集需要更长的时间才能取回数据,而我的测试"收集确实非常庞大.我需要一个具有恒定时间响应的查询.

The problem is longer the collection is the time to get back the data and my 'test' collection is really really huge. I need a query with constant time response.

如果有更好的mongodb命令行查询,请告诉我.

If there is any better mongodb command line query, do let me know.

推荐答案

这是对先前答案的重新阐述,但更可能适用于不同的mongodb版本.

This is a rehash of the previous answer but it's more likely to work on different mongodb versions.

db.collection.find().limit(1).sort({$natural:-1})

这篇关于获取mongodb集合的最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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