Example usage for com.mongodb Mongo getDB

List of usage examples for com.mongodb Mongo getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:uk.ac.ncl.aries.entanglement.WriteTestObjects.java

License:Apache License

public static void main(String[] args)
        throws UnknownHostException, JsonSerializerException, DbObjectMarshallerException {
    Mongo m = new Mongo();

    //    Mongo m = new Mongo( "localhost" );
    // or/* w  ww. ja  v a 2  s  .c om*/
    //    Mongo m = new Mongo( "localhost" , 27017 );
    // or, to connect to a replica set, supply a seed list of members
    //    Mongo m = new Mongo(Arrays.asList(new ServerAddress("localhost", 27017),
    //                                          new ServerAddress("localhost", 27018),
    //                                          new ServerAddress("localhost", 27019)));

    m.setWriteConcern(WriteConcern.SAFE);

    DB db = m.getDB("aries-test");
    //    boolean auth = db.authenticate(myUserName, myPassword);

    DBCollection testCol = db.getCollection("testCollection");

    //    BasicDBObject newDoc = new BasicDBObject();
    //    newDoc.
    //    testCol.insert(newDoc);

    Node node = new Node();
    //    node.setNumericalId(3);
    node.setUid(UidGenerator.generateUid());
    //    node.setGraphUniqueId(UidGenerator.generateUid());

    //    node.getProperties().put("An_integer", 1);
    //    node.getProperties().put("A String", "Foo");

    Map<Integer, String> subMap = new HashMap<>();
    subMap.put(1, "one");
    subMap.put(2, "two");
    subMap.put(3, "three");
    //    node.getProperties().put("A map", subMap);
    DbObjectMarshaller marshaller = ObjectMarshallerFactory.create(WriteTestObjects.class.getClassLoader());
    DBObject dbObject = marshaller.serialize(node);
    testCol.insert(dbObject);

    System.out.println("Listing collections:");
    listCollections(db);

    System.out.println("\n\nFindOne:");
    findOne(testCol);

    System.out.println("\n\nCursor iteration:");
    cursorIterateAllDocs(testCol);

    System.out.println("\n\nDone.");
}

From source file:v7db.auth.MongoAuthenticationProvider.java

License:Open Source License

public MongoAuthenticationProvider(Mongo mongo, Properties props) {
    String dbName = props.getProperty("mongo.db");
    collection = mongo.getDB(dbName).getCollection(getRequiredProperty(props, "auth.mongo.collection"));
    username_field = getRequiredProperty(props, "auth.mongo.username");
    password_field = getRequiredProperty(props, "auth.mongo.password");

}

From source file:v7db.files.buckets.BucketsServiceConfiguration.java

License:Open Source License

public void init() throws UnknownHostException, MongoException {
    Mongo mongo = Configuration.getMongo(properties);
    db = mongo.getDB(Tenants.getTenantDbName(mongo, properties, null));
}

From source file:v7db.files.mongodb.Tenants.java

License:Open Source License

/**
 * /*  w w  w . ja  v a2  s  . com*/
 * @return null, if the mapped database does not yet exist
 */
public static String getTenantDbName(Mongo mongo, Properties props, String tenantName) {
    if (props != null && "single".equals(getTenancyMode(props))) {
        return props.getProperty("mongo.db");
    }

    // TODO: some kind of mapping
    final String tenantDbName = tenantName;
    if (StringUtils.isBlank(tenantDbName))
        return null;
    if (mongo.getDB(tenantDbName).collectionExists("v7files"))
        return tenantDbName;
    return null;
}