本文介绍了MongoDB Java驱动程序3.x:如何将allowDiskUse = true传递给aggregate()方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在使用 mongo-java-driver 3.0.2 .

我有一种使用MongoCollection.aggregate(List<Bson> pipeline)进行排序和限制的方法:

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

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);
}

n大时,失败,并显示:

When n is big, it fails with:

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.'

我发现 MongoDB shell 提供了一种方法db.collection.aggregate(pipeline, options)(),其中options可以包含allowDiskUse字段.

I've found that the MongoDB shell provides a method db.collection.aggregate(pipeline, options) (link) where options can contain an allowDiskUse field.

Java API 中找不到与此等效的内容.尽管有一个 AggregationOptions 类,但MongoCollection类却没有提供aggregate(List<Bson> pipeline, AggregationOptions options)方法.

I can't find the equivalent to this in the Java API. Although there is an AggregationOptions class, the MongoCollection class doesn't provide an aggregate(List<Bson> pipeline, AggregationOptions options) method.

推荐答案

在3.0.3驱动程序上仍然有效:

This still works on the 3.0.3 driver:

    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 as well:

    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);
    }

因此 在MongoCollection上返回 AggregateIterable 类实例,该实例具有 .allowDiskuse() 方法以及其他设置聚合选项的方法.

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

这篇关于MongoDB Java驱动程序3.x:如何将allowDiskUse = true传递给aggregate()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 14:48