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] –