Example usage for com.mongodb MongoCredential MongoCredential

List of usage examples for com.mongodb MongoCredential MongoCredential

Introduction

In this page you can find the example usage for com.mongodb MongoCredential MongoCredential.

Prototype

MongoCredential(@Nullable final AuthenticationMechanism mechanism, @Nullable final String userName,
        final String source, @Nullable final char[] password) 

Source Link

Document

Constructs a new instance using the given mechanism, userName, source, and password

Usage

From source file:NegotiatedAuthenticationProtocolExample.java

License:Apache License

public static void main(String[] args) throws UnknownHostException, InterruptedException {
    String server = args[0];/*w w w . j  a  v a  2s  . co  m*/
    String user = args[1];
    String pwd = args[2];
    String db = args[3];

    MongoCredential credentials = new MongoCredential(user, pwd.toCharArray(),
            MongoAuthenticationProtocol.NEGOTIATE, db);

    MongoClient mongoClient = new MongoClient(new ServerAddress(server), Arrays.asList(credentials),
            new MongoClientOptions.Builder().build());

    DB testDB = mongoClient.getDB(db);
    testDB.getCollection("test").insert(new BasicDBObject());
    System.out.println("Count: " + testDB.getCollection("test").count());
}