获取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})