简体   繁体   English

如何在spring mongoTemplate中设置allowDiskUse:true

[英]How to set allowDiskUse:true in spring mongoTemplate

currently i am using mongoDB in spring as mongoTemplate . 目前我在春天使用mongoDB作为mongoTemplate。

public List<BasicDBObject> getMoviesByName() {
    Aggregation aggregation = newAggregation(unwind("movies"), 
            match(where("movies.language").is("english")), 
            sort(Sort.Direction.DESC, "createDate"),
            group(fields().and("_id", "$_id").and("category", "$category")).push("$movies").as("movies"),
            project(fields().and("category", "$category").and("movies", "$movies")).andExclude("_id"));
    AggregationResults<BasicDBObject> groupResults = mongoTemplate.aggregate(
            aggregation, "movieCollection", BasicDBObject.class);
    return groupResults.getMappedResults();
}

i am getting exception 我正在例外

 com.mongodb.CommandFailureException: { "serverUsed" : "XXX.XXX.XX.XX:27017" , "errmsg" : "exception: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in." , "code" : 16819 , "ok" : 0.0}

I have studied some posts allowDiskUse:true is solution ,i have tried to set true,but no result .please tell me how to set for above code 我已经研究过一些帖子allowDiskUse:true是解决方案,我试过设置true,但没有结果。请告诉我如何为上面的代码设置

You can do like below. 你可以在下面这样做。

MongoClient client = new MongoClient(new ServerAddress("127.0.0.1", 27017));

DB test = client.getDB("test");

DBCollection sample = test.getCollection("sample");

List<DBObject> aggregationQuery = Arrays.<DBObject>asList(
        new BasicDBObject("$sort",new BasicDBObject("score",-1)),
        new BasicDBObject("$limit",1)
);

System.out.println(aggregationQuery);

Cursor aggregateOutput = sample.aggregate(
        aggregationQuery,
        AggregationOptions.builder()
                .allowDiskUse(true)
                .build()
);

//rest of the code

or simply 或者干脆

    Aggregation aggregation = newAggregation(…).
        withOptions(newAggregationOptions().
        allowDiskUse(true).build());

You can set aggregation options like 您可以设置聚合选项

Aggregation aggregation = newAggregation(…).withOptions(Aggregation.newAggregationOptions().
                        allowDiskUse(true).build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM