如何使用Mongo-Java API 3在MongoCursor上设置取消超时

gid*_*dim 3 java mongodb

我正在尝试在执行find()查询时设置QUERYOPTION_NOTIMEOUT标志.该标志将覆盖MongoCursor上的默认10分钟超时.

根据Documentation find()应该返回一个DBCursor:

DBCursor cursor = collection.find(query);
Run Code Online (Sandbox Code Playgroud)

然后我就能做到

cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
Run Code Online (Sandbox Code Playgroud)

但是find()实际上返回了没有addOption()方法的FindIterableImpl /.

这是上下文的整个方法:

public static MongoCursor getSomethingFromDB(String something) {
    MongoCollection collection = getCollectionForClass(BlogPost.class);
    return collection.find(and(exists("a", false), eq("other", something))).iterator();
}
Run Code Online (Sandbox Code Playgroud)

gid*_*dim 7

在mongo-java驱动程序的版本3上执行此操作的方法是:

collection.find(and(exists("a", false), eq("other", lang))).noCursorTimeout(true).iterator();
Run Code Online (Sandbox Code Playgroud)

显然是

cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);
Run Code Online (Sandbox Code Playgroud)

来自V2 API,V3文档已过时