我想找到最近2个小时的所有文件。
@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。在mongo shell中,此查询可以正常工作:
db.events.find({'createdAt': {$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 2)}}).pretty();
答案 0 :(得分:0)
而不是
@Query("{'createdAt': {$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 2)}}")
使用以下一个
@Query("{'createdAt': { $gt: ISODate().getTime() - 1000 * 60 * 60 * 2 }}")
希望这有帮助!!!