为了账号安全,请及时绑定邮箱和手机立即绑定

MongoTemplate查询报错

MongoTemplate查询报错

你好小Song 2016-11-16 10:21:50
public List<Attendance> list(String studioCode, String studentCode, PageBean pageBean, Date startTime, Date endTime) { Query query = new Query(); if(!StringUtils.isEmpty(studioCode)){         query.addCriteria(new Criteria("studioCode").is(studioCode)); } if(!StringUtils.isEmpty(studentCode)){ query.addCriteria(new Criteria("studentCode").is(studentCode)); } if(startTime != null){ query.addCriteria(new Criteria("attendanceTime").gte(startTime)); } if(endTime != null){ query.addCriteria(new Criteria("attendanceTime").lt(endTime)); } query.skip(pageBean.getStart()).limit(pageBean.getPageSize()); return catsMongoTemplate.find(query, Attendance.class); }报错异常如下:十一月 16, 2016 10:15:49 上午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet [springmvc] in context with path [/cats] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'attendanceTime' criteria. Query already contains '{ "attendanceTime" : { "$gte" : { "$date" : "2016-10-31T16:00:00.000Z"}}}'.] with root cause org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'attendanceTime' criteria. Query already contains '{ "attendanceTime" : { "$gte" : { "$date" : "2016-10-31T16:00:00.000Z"}}}'. at org.springframework.data.mongodb.core.query.Query.addCriteria(Query.java:99) at com.hx.dao.studio.attendance.impl.AttendanceDaoImpl.list(AttendanceDaoImpl.java:46) at com.hx.service.studio.attendance.impl.AttendanceServiceImpl.list(AttendanceServiceImpl.java:33) at com.hx.controller.studio.attendance.AttendanceController.attendanceInfo(AttendanceController.java:45) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858) at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)个人感觉是时间那里的写法有问题, 因为注释query.addCriteria(new Criteria("attendanceTime").gte(startTime));或者注释query.addCriteria(new Criteria("attendanceTime").lt(endTime));就能正常查询了.请各位大侠帮我看看!
查看完整描述

1 回答

?
你好小Song

TA贡献29条经验 获得超10个赞

已解决. Criteria不允许同一字段多次出现在操作中。

修改为:

if(startTime != null && endTime != null){
    query.addCriteria(new Criteria("attendanceTime").gte(startTime).lt(endTime));
}else if(startTime == null && endTime != null){
	query.addCriteria(new Criteria("attendanceTime").lt(endTime));
}else if(startTime != null && endTime == null){
	query.addCriteria(new Criteria("attendanceTime").gte(startTime));
}

就可以了.

查看完整回答
1 反对 回复 2016-11-16
  • 1 回答
  • 1 关注
  • 7171 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信