如何获取MongoDB中字段的最大值 [英] how to get the max value of a field in MongoDB

查看:43
本文介绍了如何获取MongoDB中字段的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜欢:

{id:4563214321,updateTime:long("124354354")}

总是有新的集合进入数据库,所以我想总是获得最新更新的文档,也就是最大的更新时间.如何设计shell脚本?提前致谢.

there are always new collections enter the db, so I would like to always get latest updated documents aka the largest update time. how to design the shell script? thanks in advance.

推荐答案

您可以结合使用限制和排序来实现此目标.

You can use a combination of limit and sort to achieve this goal.

db.collectionName.find({}).sort({"updateTime" : -1}).limit(1)

这将根据更新时间对所有字段进行排序,然后只返回一个最大值.

This will sort all of your fields based on update time and then only return the one largest value.

我建议为该字段添加索引以提高性能.

I would recommend adding an index to this field to improve performance.

这篇关于如何获取MongoDB中字段的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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