質問

I wanted to use the following method of mongo java driver.\

db.<collection>.find(DBObject query,DBObject param,int skip,int batchSize,int option)

but I found this method is deprecated, so what else can be used,as I am interested in doing read operation by setting preferences.

if i use the following:

List<ServerAddress> seeds = new ArrayList<ServerAddress>();
seeds .add(seed1);
seeds .add(seed2);

MongoClient test= new MongoClient(seeds);

and later point of time if i do

test.setReadPreference(preference);

is it going to set my read preference?

役に立ちましたか?

解決

You can set the read preference on multiple levels: the client, the database, the collection and finally on individual query.

If you set the read preference on the MongoClient with

mongoClient.setReadPreference(secondary());

then this is the default read preference that will be used for all queries, unless you define otherwise. You need to ensure you set this before running any queries, or it will not take effect.

If you set a read preference on a collection using:

collection.setReadPreference(primary());

this would override your MongoClient settings, and the primary would be used by default when accessing this collection (see this blog post as well to understand how read preferences work at a collection level.

Finally, you could further override the preference for some individual queries, like the methods you found that are deprecated.

他のヒント

DBCursor has skip(), batchSize() and setOptions() methods. You can use them as follows :

db.<collection>find(DBObject query,DBObject param).skip(int skip).batchSize(int batchSize).setOptions(int options);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top