Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set allowDiskUse:true in spring mongoTemplate

currently i am using mongoDB in spring as 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

like image 478
pasham srinivasarao Avatar asked Nov 18 '16 14:11

pasham srinivasarao


People also ask

What is allowDiskUse true?

. allowDiskUse(true) enables writing temporary files to disk. . allowDiskUse(false) has no additional effect.

What is the difference between MongoOperations and MongoTemplate?

MongoTemplate provides a simple way for you to save, update, and delete your domain objects and map those objects to documents stored in MongoDB. You can save, update and delete the object as shown below. MongoOperations is the interface that MongoTemplate implements.

What is MongoTemplate used for?

MongoTemplate — MongoTemplate implements a set of ready-to-use APIs. A good choice for operations like update, aggregations, and others, MongoTemplate offers finer control over custom queries. MongoRepository — MongoRepository is used for basic queries that involve all or many fields of the document.


2 Answers

You can set aggregation options like

Aggregation aggregation = newAggregation(…).withOptions(Aggregation.newAggregationOptions().
                        allowDiskUse(true).build());
like image 23
V123 Avatar answered Nov 01 '22 13:11

V123


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());
like image 142
satish chennupati Avatar answered Nov 01 '22 13:11

satish chennupati