簡體   English   中英

如何在 MongoTemplate 中使用多個 orOperator

[英]How to use multiple orOperator in MongoTemplate

我想應用多個條件查詢運算符來根據時間戳獲取數據

public List<Notification> getNotificationByTime(long notificationTime) {

    return mongoTemplate.find(new Query(Criteria.where("createdAt").lte(notificationTime).
        orOperator(Criteria.where("updatedAt")
            .lte(notificationTime)).orOperator(Criteria.where("failureTime").gt(3))
            ), Notification.class);
  }

例外

org.quartz.SchedulerException: Job threw an unhandled exception.
    at org.quartz.core.JobRunShell.run(JobRunShell.java:213) ~[quartz-2.2.3.jar:na]
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.3.jar:na]
Caused by: org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDocument, you can't add a second '$or' expression specified as '$or : [ { "failureTime" : { "$gt" : 3}}]'. Criteria already contains '$or : [ { "updatedAt" : { "$lte" : 1549709060838}}]'.
    at org.springframework.data.mongodb.core.query.Criteria.setValue(Criteria.java:710) ~[spring-data-mongodb-2.0.10.RELEASE.jar:2.0.10.RELEASE]
    at org.springframework.data.mongodb.core.query.Criteria.getCriteriaObject(Criteria.java:643) ~[spring-data-mongodb-2.0.10.RELEASE.jar:2.0.10.RELEASE]
    at org.springframework.data.mongodb.core.query.Query.getQueryObject(Query.java:230) ~[spring-data-mongodb-2.0.10.RELEASE.jar:2.0.10.RELEASE]
    at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:771) ~[spring-data-mongodb-2.0.10.RELEASE.jar:2.0.10.RELEASE]
    at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:757) ~[spring-data-mongodb-2.0.10.RELEASE.jar:2.0.10.RELEASE]
    at io.dzone.manager.repository.impl.NotificationRepositoryImpl.getNotificationByTime(NotificationRepositoryImpl.java:21) ~[classes/:na]
    at io.dzone.manager.repository.impl.NotificationRepositoryImpl$$FastClassBySpringCGLIB$$f3a229a6.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at io.dzone.manager.repository.impl.NotificationRepositoryImpl$$EnhancerBySpringCGLIB$$c3b0276d.getNotificationByTime(<generated>) ~[classes/:na]
    at io.dzone.manager.service.impl.NotificationServiceImpl.getNotificationDataByTime(NotificationServiceImpl.java:123) ~[classes/:na]
    at io.dzone.manager.job.NotificationJob.executeInternal(NotificationJob.java:36) ~[classes/:na]
    at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75) ~[spring-context-support-5.0.9.RELEASE.jar:5.0.9.RELEASE]
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.2.3.jar:na]
    ... 1 common frames omitted
public List<Notification> getNotificationByTime(long notificationTime) {

    return mongoTemplate.find(new Query(Criteria.where("createdAt").lte(notificationTime),
        Criteria.where("updatedAt").lte(notificationTime)),Criteria.where("failureTime").gt(3)
            ), Notification.class);
  }

這必須工作。 括號內的所有內容都在進行 :)

public List<Notification> getNotificationByTime(long notificationTime) {

return mongoTemplate.find(new Query(new Criteria().andOperator(
    new Criteria().orOperator(
      Criteria.where("createdAt").lte(notificationTime),
      Criteria.where("updatedAt").lte(notificationTime)
    ),
    Criteria.where("failureTime").gt(3)), 
    Notification.class);
}

認為這一定能正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM