gpt4 book ai didi

java - 如何获取集合中的最大字段值?

转载 作者:行者123 更新时间:2023-11-30 06:46:36 24 4
gpt4 key购买 nike

我在 mongo 数据库中有一个名为 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://stackoverflow.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

最佳答案

尝试以下方法名称:findFirstByOrderByCounterDesc

https://docs.spring.io/spring-data/mongodb/docs/2.0.1.RELEASE/reference/html/

关于java - 如何获取集合中的最大字段值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47438745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com