MongoDB 3.x Java driver: How do I pass the allowDiskUse = true method to the aggregate () method?

I am using mongo-java-driver 3.0.2 .

I have a method that uses MongoCollection.aggregate(List<Bson> pipeline) to sort and restrict:

 private static MongoIterable<Document> selectTop(int n) { BasicDBObject sortFields = new BasicDBObject("score", -1); BasicDBObject sort = new BasicDBObject("$sort", sortFields); BasicDBObject limit = new BasicDBObject("$limit", n); List<BasicDBObject> pipeline = new ArrayList<>(); pipeline.add(sort); pipeline.add(limit); return playersCollection.aggregate(pipeline); } 

When n large, it fails:

 com.mongodb.MongoCommandException: Command failed with error 16820: 'exception: Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.' 

I found that the MongoDB shell provides the db.collection.aggregate(pipeline, options) ( link ) method, where options may contain allowDiskUse >.

I cannot find the equivalent of this in the Java API . Although the AggregationOptions class exists, the MongoCollection class MongoCollection not provide an aggregate(List<Bson> pipeline, AggregationOptions options) method aggregate(List<Bson> pipeline, AggregationOptions options) .

+7
java mongodb
source share
1 answer

This still works with driver 3.0.3:

  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() ); while ( aggregateOutput.hasNext() ) { DBObject doc = aggregateOutput.next(); System.out.println(doc); } 

Of course, you can also use newer classes:

  MongoClient client = new MongoClient(new ServerAddress("192.168.2.4", 27017)); MongoDatabase db = client.getDatabase("test"); MongoCollection<Document> collection = db.getCollection("sample"); AggregateIterable<Document> result = collection.aggregate(Arrays.asList( new BasicDBObject("$sort", new BasicDBObject("score", -1)), new BasicDBObject("$limit", 1) )).allowDiskUse(true); MongoCursor<Document> cursor = result.iterator(); while (cursor.hasNext()) { Document doc = cursor.next(); System.out.println(doc); } 

So .aggregate() on MongoCollection returns an AggregateIterable , which has a .allowDiskuse() method, as well as others to set aggregation parameters.

+9
source share

All Articles