如何获取集合中的最大字段值?
Posted
技术标签:
【中文标题】如何获取集合中的最大字段值?【英文标题】:How to get max field value in collection? 【发布时间】:2018-05-06 10:20:07 【问题描述】:我在 mongo db 中有名为 data
的集合。
spring 模型中的文档声明如下:
@Document(collection = "data")
public class DataPoint
@Id
private String id;
private Long counter;
...
我想在数据点条目中找到最大计数器,或者至少在 DataPoint
中找到最大值。
我试着写:
@Repository
public interface DataPointRepository extends MongoRepository<DataPoint, String>
DataPoint findFirstOrderByCounter();
在这种情况下,我在日志中看到:
org.springframework.data.repository.query.ParameterOutOfBoundsException: Invalid parameter index! You seem to have declare too little query method parameters!
at org.springframework.data.repository.query.Parameters.getParameter(Parameters.java:207)
at org.springframework.data.repository.query.Parameters.getBindableParameter(Parameters.java:277)
at org.springframework.data.repository.query.ParametersParameterAccessor.getBindableValue(ParametersParameterAccessor.java:125)
at org.springframework.data.repository.query.ParametersParameterAccessor$BindableParameterIterator.next(ParametersParameterAccessor.java:182)
at org.springframework.data.mongodb.repository.query.ConvertingParameterAccessor$ConvertingIterator.next(ConvertingParameterAccessor.java:186)
at org.springframework.data.mongodb.repository.query.MongoQueryCreator.from(MongoQueryCreator.java:247)
at org.springframework.data.mongodb.repository.query.MongoQueryCreator.create(MongoQueryCreator.java:116)
at org.springframework.data.mongodb.repository.query.MongoQueryCreator.create(MongoQueryCreator.java:60)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
at org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.createQuery(PartTreeMongoQuery.java:85)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:84)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
我也尝试关注https://***.com/a/35482658/2674303的评论
然后写:
@Repository
public interface DataPointRepository extends MongoRepository<DataPoint, String>
@Query("select max(t.counter) from DataPoint t")
DataPoint findFirstOrderByCounter();
在这种情况下,应用程序没有启动,我在日志中看到:
select max(t.counter) from DataPoint t
^
at com.mongodb.util.JSONParser.parse(JSON.java:230)
at com.mongodb.util.JSONParser.parse(JSON.java:155)
at com.mongodb.util.JSON.parse(JSON.java:92)
at com.mongodb.util.JSON.parse(JSON.java:73)
at org.springframework.data.mongodb.repository.query.StringBasedMongoQuery$ParameterBindingParser.parseAndCollectParameterBindingsFromQueryIntoBindings(StringBasedMongoQuery.java:230)
at org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.<init>(StringBasedMongoQuery.java:93)
at org.springframework.data.mongodb.repository.query.StringBasedMongoQuery.<init>(StringBasedMongoQuery.java:72)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory$MongoQueryLookupStrategy.resolveQuery(MongoRepositoryFactory.java:169)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:436)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:221)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:277)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:263)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:117)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 42 common frames omitted
【问题讨论】:
您正在尝试将 JPQL 与非 JPA 数据存储一起使用。 试试这个findFirstByOrderByCounterDesc
【参考方案1】:
试试下面的方法名:findFirstByOrderByCounterDesc
https://docs.spring.io/spring-data/mongodb/docs/2.0.1.RELEASE/reference/html/
【讨论】:
以上是关于如何获取集合中的最大字段值?的主要内容,如果未能解决你的问题,请参考以下文章