2016-09-30 2 views
0

Ich möchte alle Dokumente für die letzten 2 Stunden finden.Federdaten MongoDB. Benutzerdefinierte @Query mit dem aktuellen Datum

@Repository 
public interface EventRepository extends MongoRepository<Event, Long> { 

    @Query("{'createdAt': {$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 2)}}") 
    List<Event> findLive(); 

} 

Aber es ist ein Fehler:

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)}} 
       ^

Ich weiß, wie es über MongoTemplate zu tun. Aber ist es möglich, es über MongoRepository zu tun?

P.S. In Mongo diese Abfrage Shell funktioniert:

db.events.find({'createdAt': {$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 2)}}).pretty(); 

Antwort

0

Statt

@Query("{'createdAt': {$gt: new Date(ISODate().getTime() - 1000 * 60 * 60 * 2)}}") 

Einsatz nach einem

@Query("{'createdAt': { $gt: ISODate().getTime() - 1000 * 60 * 60 * 2 }}") 

hoffe, das hilft !!!

+0

es nicht funktioniert: Verursacht durch: org.springframework.beans.factory.BeanCreationException: Fehler Bean mit dem Namen zu schaffen 'data3Repository': Invocation der init-Methode fehlgeschlagen; nested Ausnahme ist com.mongodb.util.JSONParseException: { 'createdAt': {$ gt. isodate() getTime() - 1000 * 60 * 60 * 5 }}^ \t bei org.springframework.beans.factory .support.AbstractAutowireCapableBeanFactory.initializeBean (AbstractAutowireCapableBeanFactory.java:1583) ~ [spring-bohnen-4.3.3.RELEASE.jar: 4.3.3.RELEASE] –

Verwandte Themen