Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cancel timeout on MongoCursor with Mongo-Java API 3

Tags:

java

mongodb

I'm trying to set the QUERYOPTION_NOTIMEOUT flag when doing a find() query. The flag overrides the default 10 minutes timeout on MongoCursor.

According to the Documentation find() should return a DBCursor:

DBCursor cursor = collection.find(query);

And then I can do

cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);

But find() actually returns FindIterableImpl / which doesn't have the addOption() method.

Here's the entire method for context:

public static MongoCursor getSomethingFromDB(String something) {
    MongoCollection collection = getCollectionForClass(BlogPost.class);
    return collection.find(and(exists("a", false), eq("other", something))).iterator();
}
like image 692
gidim Avatar asked Jul 09 '26 05:07

gidim


1 Answers

The way to do this on the version 3 of the mongo-java driver is:

collection.find(and(exists("a", false), eq("other", lang))).noCursorTimeout(true).iterator();

Apparently the

cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT);

is from the V2 API and the V3 documentation is outdated

like image 73
gidim Avatar answered Jul 11 '26 20:07

gidim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!