Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

Mongo(final MongoClientURI mongoURI, @Nullable final MongoDriverInformation mongoDriverInformation) 

Source Link

Usage

From source file:com.tristrambrasil.ladder.social.canvas.config.SocialConfig.java

License:Apache License

public @Bean MongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(
            new Mongo(environment.getProperty("mongo.hostName"),
                    environment.getProperty("mongo.portNumber", Integer.class)),
            environment.getProperty("mongo.databaseName"));
}

From source file:com.ttpod.rest.web.spring.MongoFactoryBean.java

License:Apache License

@Override
public void afterPropertiesSet() throws Exception {
    String[] host_ports = urls.split(",");
    List<ServerAddress> addList = new ArrayList<ServerAddress>(host_ports.length);
    for (String host_port : host_ports) {
        String[] kv = host_port.split(":");
        addList.add(new ServerAddress(kv[0], Integer.parseInt(kv[1])));
    }/* ww w  . j  a v a2s.c o  m*/

    if (autoSlaveOk && addList.size() > 1) {
        options.readPreference = ReadPreference.secondaryPreferred();
    }

    this.mongo = new Mongo(addList, options);
}

From source file:com.wenzani.maven.mongodb.StopMongoDb.java

License:Apache License

public void execute() throws MojoExecutionException {
    port = null == port ? "27017" : port;

    try {/*from w w w .j a  v  a2 s.com*/
        Logger.getLogger("com.mongodb").setLevel(Level.OFF);
        getLog().info("Shutting down mongodb...");
        Mongo mongo = new Mongo("127.0.0.1", Integer.valueOf(port));
        mongo.getDB("admin").command(new BasicDBObject("shutdown", 1));
        getLog().info("MongoDB shutdown. Ignore the EOFException...");
    } catch (UnknownHostException e) {
        getLog().error(ExceptionUtils.getFullStackTrace(e));
    } catch (Throwable ex) {
        //ignore
    }
}

From source file:ConecteMongoDB.MongoConnection.java

public DB getDB() {
    if (mongo == null) {
        mongo = new Mongo(HOST, PORT);
        db = mongo.getDB(DB_NAME);//  www  . j  av  a2 s. c o m
        //System.out.println("Mongo instance equals :> " + mongoInstance++);
    }
    return db;
}

From source file:conexionmongodb.frmPersona.java

/**
 * Creates new form frmPersona/*from   ww  w .j  a va 2s.  c o  m*/
 */
public frmPersona() {
    initComponents();
    try {
        Mongo mongodb = new Mongo("localhost", 27017);
        db = mongodb.getDB("dasedb");
        personas = db.getCollection("personas");
    } catch (UnknownHostException ex) {
        Logger.getLogger(frmPersona.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:dao.base.MongoConnection.java

/**
 * @return db/*from w  w w.j  av  a 2 s  .c  o m*/
 */
public DB getDB() {
    if (mongo == null) {
        try {
            mongo = new Mongo("localhost", 27017);
            db = mongo.getDB("crudbasico");
        } catch (UnknownHostException e) {
            Logger.getLogger(MongoConnection.class.getName()).log(Level.SEVERE, null, e);
        }
    }
    return db;
}

From source file:DAO.MongoConnection.java

public DB getDB() {
    if (mongo == null) {
        mongo = new Mongo(HOST, PORT);
        db = mongo.getDB(DB);/*w w  w .  j av a  2 s .  c om*/
        System.out.println("Instncias de Mongo :> " + mongoInstance++);
    }
    return db;
}

From source file:DataAccess.DAO.LoginDAO.java

public String validate(String username, String password) {

    String returnText = LOGIN_FAILURE;

    Mongo mongo = new Mongo("localhost", 27017);
    DB db = mongo.getDB("Restaurant");

    try {/* w  ww  . ja va  2  s .c om*/

        //boolean auth = db.authenticate(username, password.toCharArray());
        MongoCredential credential2 = MongoCredential.createCredential(username, "Restaurant",
                password.toCharArray());
        MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential2));

        DB db2 = mongoClient.getDB("Restaurant");
        System.out.println("Connect to database successfully");

        DBCollection coll = db2.getCollection("Users");
        System.out.println("Collection users selected successfully");
        BasicDBObject document2 = new BasicDBObject();
        document2.put("UserName", username);
        document2.put("Password", password);
        //coll.insert(document2);
        DBCursor cur2 = coll.find(document2);

        System.out.println(cur2.toString());

        while (cur2.hasNext()) {
            System.out.println(cur2.next());
        }

        System.out.println("Login is successful!");

        if (credential2 != null) {

            DBCollection table = db.getCollection("Users");

            BasicDBObject document = new BasicDBObject();
            document.put("UserName", username);
            table.insert(document);
            DBCursor cur = table.find(document);

            while (cur.hasNext()) {
                System.out.println(cur.next());
            }

            HttpSession session = SessionBean.getSession();
            session.setAttribute("UserName", user);

            System.out.println("Login is successful!");
            returnText = LOGIN_SUCCESS;

            return "admins";
        } else {

            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,
                    "Incorrect Username and Passowrd", "Please enter correct username and Password"));

            System.out.println("Login is failed!");
            return "login";
        }
        //System.out.println("Done");

        //return returnText;

    } catch (MongoException e) {
        e.printStackTrace();
    } finally {
        mongo.close();
    }
    return returnText;
}

From source file:Datos.ServerMongo.java

public void conectar() {
    try {/*from www  .ja v a  2 s. co  m*/
        Mongo mongodb = new Mongo(this.address, this.port);
        db = mongodb.getDB(this.nameDB);
        //collection = db.getCollection(nameTable);
        this.estate = true;
    } catch (UnknownHostException ex) {
        System.out.println(ex);
    }
}

From source file:de.flapdoodle.embed.mongo.tests.MongosSystemForTestFactory.java

License:Apache License

private void configureMongos() throws Exception {
    CommandResult cr;/*from   w  w  w .ja v a 2 s.c o m*/
    MongoOptions mo = new MongoOptions();
    mo.autoConnectRetry = true;
    Mongo mongo = new Mongo(
            new ServerAddress(this.config.net().getServerAddress().getHostName(), this.config.net().getPort()),
            mo);
    DB mongoAdminDB = mongo.getDB(ADMIN_DATABASE_NAME);

    // Add shard from the replica set list
    for (Entry<String, List<IMongodConfig>> entry : this.replicaSets.entrySet()) {
        String replicaName = entry.getKey();
        String command = "";
        for (IMongodConfig mongodConfig : entry.getValue()) {
            if (command.isEmpty()) {
                command = replicaName + "/";
            } else {
                command += ",";
            }
            command += mongodConfig.net().getServerAddress().getHostName() + ":" + mongodConfig.net().getPort();
        }
        logger.info("Execute add shard command: " + command);
        cr = mongoAdminDB.command(new BasicDBObject("addShard", command));
        logger.info(cr.toString());
    }

    logger.info("Execute list shards.");
    cr = mongoAdminDB.command(new BasicDBObject("listShards", 1));
    logger.info(cr.toString());

    // Enabled sharding at database level
    logger.info("Enabled sharding at database level");
    cr = mongoAdminDB.command(new BasicDBObject("enableSharding", this.shardDatabase));
    logger.info(cr.toString());

    // Create index in sharded collection
    logger.info("Create index in sharded collection");
    DB db = mongo.getDB(this.shardDatabase);
    db.getCollection(this.shardCollection).ensureIndex(this.shardKey);

    // Shard the collection
    logger.info("Shard the collection: " + this.shardDatabase + "." + this.shardCollection);
    DBObject cmd = new BasicDBObject();
    cmd.put("shardCollection", this.shardDatabase + "." + this.shardCollection);
    cmd.put("key", new BasicDBObject(this.shardKey, 1));
    cr = mongoAdminDB.command(cmd);
    logger.info(cr.toString());

    logger.info("Get info from config/shards");
    DBCursor cursor = mongo.getDB("config").getCollection("shards").find();
    while (cursor.hasNext()) {
        DBObject item = cursor.next();
        logger.info(item.toString());
    }

}