Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

private Optional<CommandResult> runDBCommand(String database, DBObject command) {
    MongoClient client = client();
    try {/*from   w w w .ja v  a  2  s .c om*/
        DB db = client.getDB(database);
        CommandResult status;
        try {
            status = db.command(command);
        } catch (MongoException e) {
            LOG.warn("Command " + command + " on " + getServerAddress() + " failed", e);
            return Optional.absent();
        }
        if (!status.ok()) {
            LOG.debug("Unexpected result of {} on {}: {}",
                    new Object[] { command, getServerAddress(), status.getErrorMessage() });
        }
        return Optional.of(status);
    } finally {
        client.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

public long getShardCount() {
    MongoClient client = client();
    try {// ww  w.j ava2 s. c o m
        return client.getDB("config").getCollection("shards").getCount();
    } finally {
        client.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBClientSupport.java

License:Apache License

/**
 * Java equivalent of calling rs.conf() in the console.
 *///  ww w.  j  a v a  2  s. c  om
private BSONObject getReplicaSetConfig() {
    MongoClient client = client();
    try {
        return client.getDB("local").getCollection("system.replset").findOne();
    } catch (MongoException e) {
        LOG.error("Failed to get replica set config on " + client, e);
        return null;
    } finally {
        client.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

/**
 * Inserts a new object with { key: value } at given server.
 * @return The new document's id//from  w w  w .  jav  a  2s  .c  om
 */
public static String insert(AbstractMongoDBServer entity, String key, Object value) {
    LOG.info("Inserting {}:{} at {}", new Object[] { key, value, entity });
    MongoClient mongoClient = clientForServer(entity);
    try {
        DB db = mongoClient.getDB(TEST_DB);
        DBCollection testCollection = db.getCollection(TEST_COLLECTION);
        BasicDBObject doc = new BasicDBObject(key, value);
        testCollection.insert(doc);
        ObjectId id = (ObjectId) doc.get("_id");
        return id.toString();
    } finally {
        mongoClient.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

/** @return The {@link DBObject} representing the object with the given id */
public static DBObject getById(AbstractMongoDBServer entity, String id) {
    LOG.info("Getting {} from {}", new Object[] { id, entity });
    MongoClient mongoClient = clientForServer(entity);
    // Secondary preferred means the driver will let us read from secondaries too.
    mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
    try {//  www .j a  va 2  s . co m
        DB db = mongoClient.getDB(TEST_DB);
        DBCollection testCollection = db.getCollection(TEST_COLLECTION);
        return testCollection.findOne(new BasicDBObject("_id", new ObjectId(id)));
    } finally {
        mongoClient.close();
    }
}

From source file:brooklyn.entity.nosql.mongodb.MongoDBTestHelper.java

License:Apache License

public static boolean isConfigServer(AbstractMongoDBServer entity) {
    LOG.info("Checking if {} is a config server", entity);
    MongoClient mongoClient = clientForServer(entity);
    try {//from   w w  w . j  a v  a 2  s  . co  m
        DB db = mongoClient.getDB(ADMIN_DB);
        CommandResult commandResult = db.command("getCmdLineOpts");
        Map<?, ?> parsedArgs = (Map<?, ?>) commandResult.get("parsed");
        if (parsedArgs == null)
            return false;
        Boolean configServer = (Boolean) parsedArgs.get("configsvr");
        if (configServer != null) {
            // v2.5 format
            return Boolean.TRUE.equals(configServer);
        } else {
            // v2.6 format
            String role = (String) ((Map) parsedArgs.get("sharding")).get("clusterRole");
            return "configsvr".equals(role);
        }
    } finally {
        mongoClient.close();
    }
}

From source file:bruma.tools.Isis2Mongo.java

License:Open Source License

private static DB getDatabase(final String host, final int port, final String user, final String password,
        final String mongoDb) throws IOException {
    final MongoClient mongo = new MongoClient(host, port);
    final DB db = mongo.getDB(mongoDb);

    if (user != null) {
        if (!db.authenticate(user, password.toCharArray())) {
            throw new IOException("database authentication failed");
        }/*from w  w w  . ja  va  2  s  .c om*/
    }
    return db;
}

From source file:buysell.login.java

private void loginSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginSActionPerformed
    // TODO add your handling code here:
    shopping sh1 = new shopping();
    try {/*from  ww w .j a v a 2 s . c  om*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("snehal");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("user");
        System.out.println("Collection created successfully");
        String name = jTextField1.getText().toString();
        String passwd = jPasswordField1.getText().toString();
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", name);
        searchQuery.put("passwd", passwd);

        DBCursor cursor = coll.find(searchQuery);
        int i = 0;
        while (cursor.hasNext()) {
            i = 1;
            System.out.println(cursor.next());
        }
        if (i == 0) {
            JOptionPane.showMessageDialog(null, "user name or passwod is wrong");

        } else {
            System.out.println("user get");
            sh1.setVisible(true);
            dispose();
        }
        System.out.println("Document inserted successfully");
    } catch (NumberFormatException e) {
        System.out.println();
    }

}

From source file:buysell.shopping.java

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    // TODO add your handling code here:

    try {/*from   w w  w .j a v a 2  s. c  o  m*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("buysale");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("assetes");
        System.out.println("Collection created successfully");
        //String name = jTextField1.getText().toString();
        //String email = jTextField2.getText().toString();
        //String phoneno = jTextField3.getText().toString();
        // String price= jTextField4.getText().toString();
        //String photourl = jTextField5.getText().toString();
        String check = jCheckBox1.getText().toString();
        System.out.println(check);
        String type = jComboBox1.getSelectedItem().toString();
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("type", type);
        DBCursor cursor = coll.find(searchQuery);
        int i = 0;
        while (cursor.hasNext() && i != 1) {
            i++;
            DBObject obj = cursor.next();
            String name = (String) obj.get("name");
            String email = (String) obj.get("email");
            String phoneno = (String) obj.get("phoneno");
            String price = (String) obj.get("price");
            String url = (String) obj.get("photourl");
            jTextField1.setText(name);
            jTextField2.setText(email);
            jTextField3.setText(phoneno);
            jTextField4.setText(price);
            jLabel3.setIcon(new javax.swing.ImageIcon(url));
        }

        System.out.println("Document inserted successfully");
    } catch (NumberFormatException e) {
        System.out.println();
    }
}

From source file:buysell.sign.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:

    try {/*from w w w  . ja  v  a  2  s .c  o  m*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("snehal");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("user");
        System.out.println("Collection created successfully");
        String fullname = jTextField2.getText().toString();

        String email = jTextField3.getText().toString();
        String passwd = jTextField4.getText().toString();
        System.out.println("finalproject.sign.jButton1ActionPerformed()" + fullname);
        if (jTextField2.getText().length() != 0 && jTextField3.getText().length() != 0
                && jTextField4.getText().length() != 0) {
            BasicDBObject doc = new BasicDBObject("name", fullname).

                    append("passwd", passwd);

            coll.insert(doc);

            System.out.println("Document inserted successfully");
            home h1 = new home();
            h1.setVisible(true);
            dispose();
        } else {
            JOptionPane.showMessageDialog(null, "Fill every field");
        }
    } catch (NumberFormatException e) {
        System.out.println();
    }
}