2016-01-20 122 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>}) 
相關問題