java - Spring数据排序操作超出最大大小

标签 java spring mongodb spring-data

我是 Spring 和 Mongodb 新手,在从 MongoDB 中提取数据时遇到问题。我正在尝试获取相当大量的数据,但收到以下异常:

  ... 
   Query query = new Query();
    query.with(new Sort(Sort.Direction.DESC, "vin"));
        Criteria c = new Criteria().andOperator(Criteria.where("updateTime").gt(startDate),  
                Criteria.where("updateTime").lte(endDate));  

        query.addCriteria(c);

        return this.mongoOperations.find(query, VehicleStatus.class);
  • 执行程序错误:操作失败:排序操作使用的 RAM 超过最大 33554432 字节。添加索引,或指定较小的限制。;嵌套异常是 com.mongodb.MongoException:执行器错误:OperationFailed:排序操作使用的 RAM 超过最大 33554432 字节。添加索引,或指定较小的限制。

进一步阅读后,似乎使用聚合是一个好主意,因为我可以允许使用磁盘进行临时存储。我认为允许磁盘空间可以解决这个问题,也许我使用不正确?但是我现在收到以下错误:聚合结果超出最大文档大小 (16MB)",“代码”:16389

 ...
 MatchOperation matchStage = Aggregation.match(new Criteria().andOperator(Criteria.where("updateTime").gt(startDate),  
                Criteria.where("updateTime").lte(endDate)));
        SortOperation sort = Aggregation.sort(Direction.DESC, "vin");

        Aggregation agg = Aggregation.newAggregation(VehicleStatus.class, matchStage, sort)
                .withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());

        List<VehicleStatus> vs = this.mongoOperations.aggregate(agg, "vehicleStatus", VehicleStatus.class).getMappedResults();
        return vs;

我真的不知道如何处理这个问题。如果两个查询都在给定的大小限制内,则它们都可以工作。如果有人有任何提示或建议,我们将不胜感激。如果您有任何疑问或需要任何其他代码,请告诉我!

谢谢

最佳答案

聚合管道的结果超过16MB,并且由于MongoDB文档允许的最大大小为16MB,因此抛出此异常。

所以,你有两个选择:

  • 减少输出的大小,一种可能是使用 Spring Data MongoDB 的 limit() 方法来限制输出的大小
  • 使用 MongoDB 的 $out operator将聚合结果通过管道传输到集合

您可以调整您的代码以通过 Spring 的 Mongo DB 外观使用 $out (注意:我尚未验证此代码,但重要的一点是您必须提供 AggregationOperation 就像我在下面编写的代码一样):

Aggregation agg = Aggregation.newAggregation(VehicleStatus.class, matchStage, sort, new    AggregationOperation() {
  @Override
  public DBObject toDBObject(AggregationOperationContext context) {
    return new BasicDBObject("$out", “outputCollection”);
  }
}).withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());

关于java - Spring数据排序操作超出最大大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45331032/

相关文章:

java - 连接 "computer-database-jpa"Play 2.1 示例应用程序与 MySQL

java - 如何知道是否对 jtextarea 进行了任何更改?

java - 用户身份验证失败时出现 Spring Security ProviderNotFoundException

java - 创建 bean 'entityManagerFactory' 时出错,嵌套的 HibernateException : Unable to get the default Bean Validation factory

mysql - MongoDB 写关注同步级别

mongodb - PyMongo:如何使用聚合更新集合?

java - 频闪灯不能正常工作

java - PatternSyntaxException : while using String. java中的ReplaceAll函数?

java - Web 启用 Java 控制台应用程序的 JMX bean

node.js - 索引多个 MongoDB 字段,只使一个唯一