2016-01-20 123 views
2

我想在使用Mongo Java Driver 3.0.4的大集合上运行聚合。在我的收藏的小样本上,一切都很好,但是当我试图在整个系列上执行它时,我最终以MongoCursorNotFoundException结束。我发现这是一个Cursor超时并被服务器关闭的问题。MongoDB Java驱动程序 - 如何禁用聚合查询中的游标超时?

但是,我不明白如何设置此选项。 aggregate()函数返回一个AggregateIterable,它只有useCursor方法,似乎有点相关。另一方面,find()函数返回FindIterable,它有一个方便的方法noCursorTimeout(Boolean)。我不明白为什么它很容易找到,但没有明显的方式来聚合这个选项。我应该如何确保游标在一段时间后不会失败?

我的代码到目前为止是这样的。

AggregateIterable<Document> iterable = db.getCollection("tweets").aggregate(asList(new Document("$sort", new Document("timestamp_ms", 1)), 
     new Document("$group", new Document("_id", "$relatedTrend") 
          .append("count", new Document("$sum", 1)) 
          .append("tweets", new Document("$push", new Document("timestamp_millis", "$timestamp_ms")))))).allowDiskUse(true); 

iterable.forEach(new Block<Document>() { 
    @Override 
    public void apply(final Document document) { 
    //parse field "tweets" of document and do a lot of calculations. 
    } 
}); 

回答

4

选中此MongoDB JIRA ticket

mongod --setParameter cursorTimeoutMillis=<num> 

mongos --setParameter cursorTimeoutMillis=<num> 

如果这是不是一种选择,你也可以运行以下shell命令:

开始的 mongodmongos实例时,您可以增加闲置光标超时
use admin 
db.runCommand({setParameter:1, cursorTimeoutMillis: <num>}) 
相关问题