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

查看:278
本文介绍了从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天全站免登陆