我正在使用MongoOptions
类及其方法
setFsync(boolean sync)
setJ(boolean safe)
setW(int val)
setWtimeout(int timeoutMS)
setSafe(boolean isSafe)
如何使用MongoClientOptions
作为MongoOptions
实现此目的,在 Mongo-Java-Driver 3.0 中被删除。我知道MongoClientOptions
使用
MongoClientOptions.builder()
创建一个新的Builder实例,然后附加属性。
答案 0 :(得分:4)
在构建器上使用writeConcern方法,如:
MongoClientOptions options = MongoClientOptions.builder()
.writeConcern(WriteConcern.JOURNALED)
.build();
或
答案 1 :(得分:1)
您可以使用如下:您可以使用新Mongoclient的对象设置读取首选项和写入关注...有可用的api列表。请检查以下格式..
MongoClient c = new MongoClient(new MongoClientURI("mongodb://localhost"));
DB db = c.getDB("final");
DBCollection animals = db.getCollection("emp");
BasicDBObject animal = new BasicDBObject("emp", "john");
MongoClientOptions options = new MongoClient().setReadPreference(preference);
MongoClientOptions options = new MongoClient().setWriteConcern(concern);
您可以添加fsynk以及..
MongoClientOptions options = new MongoClient().fsync(async)
答案 2 :(得分:1)
客户端版本3.6的情况要复杂得多。您必须实例化WriteConcern并将其与MongoClientOptions.Builder一起使用。例如:
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;
import com.mongodb.WriteConcernError;
public class MongoOptionsSample
{
public static void main( String[] args )
{
WriteConcern l_concern = new WriteConcern( wVal, wTimeoutMS )
.withJournal( bool );
MongoClientOptions l_opts =
MongoClientOptions
.builder()
.writeConcern( l_concern )
.build();
ServerAddress l_addr = new ServerAddress( "localhost", 27017 );
try
(
MongoClient l_conn = new MongoClient( l_addr, l_opts );
)
{
...
}
}
}
不推荐使用Fsync和safe。有关详细信息,请参阅WriteConcern文档。