0
我想查找最近2个小时的所有文档。Spring Data MongoDB。用当前日期定制@Query
@Repository
public interface EventRepository extends MongoRepository<Event, Long> {
@Query("{'createdAt': {$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 2)}}")
List<Event> findLive();
}
但有一个错误:
09-30 14:06:33 WARN org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'actionController' defined in file [/Users/serge/projects/bb/bb-whlive/target/classes/bb/whlive/controller/ActionController.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eventService' defined in file [/Users/serge/projects/bb/bb-whlive/target/classes/bb/whlive/service/EventService.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'eventRepository': Invocation of init method failed; nested exception is com.mongodb.util.JSONParseException:
{'createdAt': {$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 2)}}
^
我知道如何通过MongoTemplate做到这一点。但是有可能通过MongoRepository来实现吗?
P.S.在蒙戈外壳此查询工作正常:
db.events.find({'createdAt': {$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 2)}}).pretty();
它不起作用: 引起:org.springframework.beans.factory.BeanCreationException:创建名为'data3Repository'的bean时出错:调用init方法失败;嵌套的异常是com.mongodb.util.JSONParseException: {'createdAt':{$ gt:ISODate()。getTime() - 1000 * 60 * 60 * 5}} ^ \t at org.springframework.beans.factory .AuthenticationAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)〜[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE] –