如何使用MongoTemplate按顺序获取结果?

时间:2018-10-24 08:24:45

标签: java mongodb spring-boot

在mongoDB中,我想按顺序获取结果,但是当我使用sort方法时,错误

  

com.mongodb.MongoQueryException:查询失败,错误代码为96并且   错误消息“在查找命令期间执行器错误::由::排序引起   操作使用的内存超过了最大的33554432字节RAM。添加一个   索引,或指定一个较小的限制。”在服务器localhost:27017上   com.mongodb.operation.FindOperation $ 1.call(FindOperation.java:722)在   com.mongodb.operation.FindOperation $ 1.call(FindOperation.java:711)在   com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:471)     在   com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:415)     在   com.mongodb.operation.FindOperation.execute(FindOperation.java:711)     在com.mongodb.operation.FindOperation.execute(FindOperation.java:83)     在com.mongodb.Mongo $ 3.execute(Mongo.java:826)处   com.mongodb.MongoIterableImpl.execute(MongoIterableImpl.java:130)在   com.mongodb.MongoIterableImpl.iterator(MongoIterableImpl.java:77)位于   com.linkyoyo.wmlink.service.impl.DataShowServiceImpl.getAllDateRWDByTowerId(DataShowServiceImpl.java:42)     在   com.linkyoyo.wmlink.service.impl.DataShowServiceImpl.getSpeedDateRWDByTowerId(DataShowServiceImpl.java:49)     在   com.linkyoyo.wmlink.service.impl.DataShowServiceImpl $$ FastClassBySpringCGLIB $$ 75f7873c.invoke()     在   org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)     在   org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)     在   org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)     在   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)     在   org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)     在   com.linkyoyo.wmlink.service.impl.DataShowServiceImpl $$ EnhancerBySpringCGLIB $$ eaf288ce.getSpeedDateRWDByTowerId()     在   com.linkyoyo.wmlink.controller.DataShowControllerTest.allData(DataShowControllerTest.java:44)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在   org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)     在   org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)     在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)     在   org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)     在   org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)     在   org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)处   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)     在org.junit.runners.ParentRunner处$ 3.run(ParentRunner.java:290)在   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)在   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)在   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)在   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)在   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)处   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)     在   org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)     在   org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)     在   org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

发生了。

public MongoCursor<Document> getAllDateRWDByTowerId(Integer towerId) {

    MongoCollection<Document> mongoCollection = mongoTemplate.getCollection("rwd");

    FindIterable<Document> findIterable = collection.find().sort(Sorts.orderBy(Sorts.descending("date")));

    return fi.iterator();
}

我尝试了几种不同的方法,总是会发生相同的错误。我不知道为什么会这样以及如何解决这个问题。

2 个答案:

答案 0 :(得分:1)

您应该尝试为对象的字段“日期”添加索引, 如果不添加它,mongo将从内存中的硬盘加载很多数据 并导致异常。

如果您知道index,则在某些数据库中索引是由B +数据结构实现的, 这是一个硬盘友好的数据结构,使用更少的内存。

答案 1 :(得分:1)

错误是:

Sort operation used more than the maximum 33554432 bytes of RAM

蒙哥Sort Operations

  

如果MongoDB无法使用索引来获取所需排序的文档   顺序,排序操作中所有文档的合并大小,再加上   很小的开销,必须小于32兆字节。

解决方案是将索引添加到排序字段。有关更多详细信息,请参见here