java - MongoDB Java 驱动程序 3.x : How to pass allowDiskUse=true to aggregate() method?

标签 java mongodb

我正在使用 mongo-java-driver 3.0.2

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

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很大,它失败了:

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) ( link ) 其中 options可以包含 allowDiskUse字段。

我在 Java API 中找不到与此等效的内容。虽然有一个 AggregationOptions类,MongoCollection类不提供 aggregate(List<Bson> pipeline, AggregationOptions options)方法。

最佳答案

这仍然适用于 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);
    }

当然你也可以使用更新的类:

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

所以 .aggregate()在 MongoCollection 上返回 AggregateIterable类实例,它有一个 .allowDiskuse()方法以及其他设置聚合选项的方法。

关于java - MongoDB Java 驱动程序 3.x : How to pass allowDiskUse=true to aggregate() method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31283479/

相关文章:

java - 从 Scala 函数到 Java 函数的隐式转换

java - 当有多个同名元素时,如何删除确切的元素?

java - 测试方法失败并出现日志错误

php - 为什么我不能在 MongoDB 文档中存储关联数组?

javascript - MongoDB 索引 : String vs Int

mongodb - 在 Javascript (Meteor) 中将字符串转换为 Mongo ObjectID

java - 嵌套的 Android 应用程序无法启动

java - 什么是 ParallelStream 队列行为?

java - Android MediaPlayer 重置卡住 UI

node.js - Mongo/Mongoose 聚合 - $redact 和 $cond 问题