从文件中查找最大值的 MongoTemplate 方法或查询 [英] MongoTemplate method or query for finding maximum values from a fileds

查看:680
本文介绍了从文件中查找最大值的 MongoTemplate 方法或查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 MongoTemplate 用于我的数据库操作.现在我想从所选结果中获取最大字段值.有人可以指导我如何编写查询,以便当我将查询传递给 find 方法时,它将返回我所需的最大文档字段.提前致谢问候

I am using MongoTemplate for my DB operations. Now i want to fetch the maximum fields values from the selected result. Can someone guide me how i write the query so that when i pass the query to find method it will return me the desired maximum fields of document . Thanks in advance Regards

推荐答案

在 spring-data-mongodb 中可以找到字段值最大的对象".如果排序字段被索引(或@Id 字段),Mongo 将优化排序/限制组合.否则它仍然很不错,因为它将使用 top-k 算法并避免全局排序(mongodb 排序文档).这是来自 Mkyong 的例子 但我先进行排序并将限制设置为一秒.

You can find "the object with the maximum field value" in spring-data-mongodb. Mongo will optimize sort/limit combinations IF the sort field is indexed (or the @Id field). Otherwise it is still pretty good because it will use a top-k algorithm and avoid the global sort (mongodb sort doc). This is from Mkyong's example but I do the sort first and set the limit to one second.

Query query = new Query();
query.with(new Sort(Sort.Direction.DESC, "idField"));
query.limit(1);
MyObject maxObject = mongoTemplate.findOne(query, MyObject.class);

这篇关于从文件中查找最大值的 MongoTemplate 方法或查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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