Example usage for com.mongodb MongoURI connect

List of usage examples for com.mongodb MongoURI connect

Introduction

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

Prototype

@SuppressWarnings("deprecation")
public Mongo connect() 

Source Link

Document

Creates a Mongo instance based on the URI.

Usage

From source file:org.ingini.monogo.testbed.MongoManager.java

License:Apache License

/**
 * Tries to connect to a given running MongoDB instance at {@code uri}
 * <p>Example URIs:</p>/*  w  w  w .  j a va2  s .  c  om*/
 *      *) mongodb://127.0.0.1:27017 - connecting to 127.0.0.1 @ port 27017 without authenticating
 *      *) mongodb://john:doe@127.0.0.1:27017 - connecting to 127.0.0.1 @ port 27017 with username: 'john' and password: 'doe'
 *      *) mongodb://john:@127.0.0.1:27017 - connecting to 127.0.0.1 @ port 27017 with username: 'john' and empty password
 * @param uri
 * @throws IllegalStateException in case of difficulties while connecting
 */
private MongoManager(String uri) {
    MongoURI mongoURI = new MongoURI(uri);
    try {
        this.mongo = mongoURI.connect();
    } catch (UnknownHostException e) {
        logger.error("Could not connect to {} due to an exception!", uri, e);
        throw new IllegalStateException(e);
    }
    this.mongoDB = mongo.getDB(MONOGO_TESTBED_DB);
    if (mongoURI.getUsername() != null) {
        this.mongoDB.authenticate(mongoURI.getUsername(), mongoURI.getPassword());
    }
}