我在mongo db中有名为data
的集合。
春季模特中的文件声明像:
@Document(collection = "data")
public class DataPoint {
@Id
private String id;
private Long counter;
...
}
我希望在datapoint条目中找到最大计数器,或者至少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
答案 0 :(得分:2)
请尝试以下方法名称:findFirstByOrderByCounterDesc
https://docs.spring.io/spring-data/mongodb/docs/2.0.1.RELEASE/reference/html/