Stack Exchange
Error with SpringData MongoDB and criteria query where same field occurs twice
I have the following query to get documents from a collection:
public List<Event> findSubEventsBetweenDates(Event parentEvent, LocalDate lowerDate, LocalDate upperDate) {

    return mongo.find(query(
            where("parentEvent").is(parentEvent)
                    .and("startDate").gte(lowerDate)
                    .and("startDate").lt(upperDate)
            ), Event.class);
}

When executing this exception is thrown:
java.lang.RuntimeException: json can't serialize type : class org.joda.time.LocalDate
com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:77)
com.mongodb.util.JSONSerializers$MapSerializer.serialize(JSONSerializers.java:307)
com.mongodb.util.ClassMapBasedObjectSerializer.serialize(ClassMapBasedObjectSerializer.java:79)
com.mongodb.util.JSON.serialize(JSON.java:56)
com.mongodb.util.JSON.serialize(JSON.java:41)
com.mongodb.BasicDBObject.toString(BasicDBObject.java:83)
java.lang.String.valueOf(String.java:2854)
java.lang.StringBuilder.append(StringBuilder.java:128)
org.springframework.data.mongodb.core.query.Criteria.setValue(Criteria.java:523)
org.springframework.data.mongodb.core.query.Criteria.getCriteriaObject(Criteria.java:470)
org.springframework.data.mongodb.core.query.Query.getQueryObject(Query.java:204)
org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:532)
org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:523)
net.wombaz.tango.model.dao.EventDAO.findSubEventsBetweenDates(EventDAO.java:114)
net.wombaz.tango.service.EventService.getSubEventsBetweenDates(EventService.java:289)
net.wombaz.tango.service.EventService$$FastClassByCGLIB$$d4d30570.invoke(<generated>)
org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:639)
net.wombaz.tango.service.EventService$$EnhancerByCGLIB$$361caa89.getSubEventsBetweenDates(<generated>)
net.wombaz.tango.service.RepeatingEventService.updateRepeatingEventInstances(RepeatingEventService.java:76)
net.wombaz.tango.controller.EventController.editRepeatingEventPost(EventController.java:399)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:601)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:105)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)

The problem does not occur when one of the two LocalDate criterias is not used, e.g.
public List<Event> findSubEventsBetweenDates(Event parentEvent, LocalDate lowerDate, LocalDate upperDate) {

    return mongo.find(query(
            where("parentEvent").is(parentEvent)
                    .and("startDate").gte(lowerDate)
                    //.and("startDate").lt(upperDate)
            ), Event.class);
}

It does not matter which one of the two is commented out. So the problem must be, that there are two criterias of the same field. Has anybody a hint for me?

Have you tried new Criteria().andOperator(where("parentEvent").is(parentEvent), where("startDate").gte(lowerDate), where("startDate").lt(upperDate))?

Yes, this works. Thank you. Seems to be different how SpringData creates the queries although it should be the same.

Thanks! I first think should do some convertor work explicitly, e.g.  LocalDate --> Date

Use 
new Criteria().andOperator(
    where("parentEvent").is(parentEvent), 
    where("startDate").gte(lowerDate), 
    where("startDate").lt(upperDate))

instead.

If you change LocalDate to String , you'll find more clear error message.
match(where("registerTime").gte(start.toString()).and("registerTime").lt(end.toString()))

then in this case the error message is 
org.springframework.data.mongodb.InvalidMongoDbApiUsageException: Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'registerTime' expression specified as 'registerTime : { "$lt" : "2017-06-11"}'. Criteria already contains 'registerTime : { "$gte" : "2017-06-10"}'.

    at org.springframework.data.mongodb.core.query.Criteria.setValue(Criteria.java:688)

