Announcement Announcement Module
Collapse
No announcement yet.
Group with MongoTemplate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Group with MongoTemplate

    Hello

    I seem to be having a difficulty finding a proper example of a MongoTemplate.group(..) operation usage.

    I have a collection that I would like to group by 2 fields and return one of them with the count of each group (that is, also a different projection).

    On Mongo console the query is pretty simple:
    Code:
    myDb.myCollection.group({key: { fld1:1, fld2:1 }, reduce: function(obj, prev) { prev.counter += 1; }, initial: { counter: 0 }})
    This works, but I can't translate this into a MongoTemplate.group(..) call.

    Following call throws an IllegalArgumentException from MongoTemplate:
    Code:
    GroupBy groupBy = GroupBy.key("fld1", "fld2").initialDocument("{ counter: 0 }").reduceFunction("function(obj, prev) { prev.counter++ ; }");
    GroupByResults<MyResult> resCount = mongoTemplate.group("myCollection", groupBy, MyResult.class);
    Stacktrace:
    Code:
    java.lang.IllegalArgumentException: No property count found for type class java.lang.Integer
    	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:73)
    	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:92)
    	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:312)
    	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:294)
    	at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:258)
    	at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:95)
    	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:312)
    	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:326)
    	at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:294)
    	at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:258)
    	at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:239)
    	at org.springframework.data.mongodb.core.convert.MappedConstructor$MappedParameter.<init>(MappedConstructor.java:125)
    	at org.springframework.data.mongodb.core.convert.MappedConstructor.<init>(MappedConstructor.java:57)
    	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:212)
    	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:200)
    	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:164)
    	at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:73)
    	at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:1693)
    	at org.springframework.data.mongodb.core.MongoTemplate.group(MongoTemplate.java:1094)
    	at org.springframework.data.mongodb.core.MongoTemplate.group(MongoTemplate.java:1031)
    	at com.mycompany.myproject.MainController.doSomething(MainController.java:78)
    	at com.mycompany.myproject.MainController.doSomething(MainController.java:71)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:597)
    	at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
    	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
    	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
    	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
    	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:301)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:162)
    	at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    	at java.lang.Thread.run(Thread.java:662)
    Any ideas?

    Thanks,
    Yuval
    Last edited by YuvalRon; Jul 22nd, 2012, 02:44 PM.

  • #2
    Solved. Still think there is a lack of documentation on MongoTemplate, especially examples that can save time.

    Comment


    • #3
      The testsuite that is included in the source dist has some examples, in org.springframework.data.mongodb.core.mapreduce.Gr oupByTests.

      HTH, Thomas

      Comment

      Working...
      X