How to use MongoClientOptions instead of MongoOptions?

I use the class MongoOptionsand its methods

setFsync(boolean sync)

setJ(boolean safe)

setW(int val)

setWtimeout(int timeoutMS)

setSafe(boolean isSafe)

How to achieve this using MongoClientOptionshow MongoOptionsis degraded in Mongo-Java-Driver 3.0 . I found out that MongoClientOptionsuses

MongoClientOptions.builder ()

to create a new instance of Builder, and then add properties.

+4
source share
3 answers

Use the writeConcern method in the builder, for example:

MongoClientOptions options = MongoClientOptions.builder()
                                               .writeConcern(WriteConcern.JOURNALED)
                                               .build();

or

+3
source

, : read prefernece Mongoclient... apis. , .

        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)
+1

With client version 3.6, the situation is rather complicated. You will need to create WriteConcern and use it with MongoClientOptions.Builder . Example:

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 and safe are deprecated. See the WriteConcern documentation for more details .

0
source

All Articles