Example usage for com.mongodb MongoURI MongoURI

List of usage examples for com.mongodb MongoURI MongoURI

Introduction

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

Prototype

@SuppressWarnings("deprecation")
@Deprecated
public MongoURI(final MongoClientURI proxied) 

Source Link

Document

Create a new MongoURI from a MongoClientURI.

Usage

From source file:org.wrml.contrib.runtime.service.mongo.MongoService.java

License:Apache License

@Override
protected void initFromConfiguration(final ServiceConfiguration config) {

    if (config == null) {
        final ServiceException e = new ServiceException("The config cannot be null.", null, this);
        LOG.error(e.getMessage(), e);/*from w  w  w. j av a2 s . c o m*/
        throw e;
    }

    final Map<String, String> settings = config.getSettings();
    String mongoUriString = DEFAULT_URI_STRING;

    if (settings != null) {
        if (settings.containsKey(MONGO_URI_SETTING_NAME)) {
            mongoUriString = settings.get(MONGO_URI_SETTING_NAME);
        }

        if (settings.containsKey(MONGO_COLLECTION_PREFIX_SETTING_NAME)) {
            _CollectionPrefix = settings.get(MONGO_COLLECTION_PREFIX_SETTING_NAME);
        }
    }

    // TODO: Look into MongoClientURI replacement
    final MongoURI mongoUri = new MongoURI(mongoUriString);
    try {
        _Mongo = mongoUri.connectDB();

        if (!_Mongo.isAuthenticated() && mongoUri.getPassword() != null) {
            _Mongo.authenticate(mongoUri.getUsername(), mongoUri.getPassword());
        }

    } catch (MongoException | UnknownHostException ex) {
        final String logMessage = "Error creating connection to Mongo: " + _Mongo;
        LOG.error(logMessage);
        throw new ServiceException(logMessage, ex, this);
    }
}

From source file:others.Capped.java

License:Apache License

public static void main(String[] args) throws MongoException, UnknownHostException {

    MongoURI uri = new MongoURI("mongodb://10.11.0.52:27017/test");
    DB db = new Mongo(uri).getDB("test");
    DBObject foo = new BasicDBObject();
    foo.put("create", "capped1");
    foo.put("capped", true);
    foo.put("size", 100000000);
    DBObject dbobj = db.command(foo);//from w w w. ja v  a 2 s .  c o m
    DBCollection c = db.getCollection("capped1");

    DBObject obj = new BasicDBObject();
    Date begin = new Date();
    for (int i = 1; i <= 1000000; i++) {
        obj.removeField("x");
        obj.put("x", i);
        c.insert(obj);
    }
    Date end = new Date();
    System.out.println("One by one:" + ((end.getTime() - begin.getTime()) / 1000));

    DBObject foo2 = new BasicDBObject();
    foo.put("create", "capped2");
    foo.put("capped", true);
    foo.put("size", 100000000);
    DBObject dbobj2 = db.command(foo);
    DBCollection c2 = db.getCollection("capped2");

    begin = new Date();
    for (int i = 1; i <= 1000; i++) {
        List<DBObject> list = new ArrayList<DBObject>(1000);
        for (int j = 1; j <= 1000; j++) {
            DBObject dbo = new BasicDBObject();
            dbo.put("x", j + (i - 1) * 1000);
            list.add(dbo);
        }
        c2.insert(list);
    }
    end = new Date();
    System.out.println("Batch(per 1000):" + ((end.getTime() - begin.getTime()) / 1000));
}

From source file:spring.boot.nomaven.ConfiguracionMongo.java

@Bean
public SimpleMongoDbFactory mongoDbFactory() throws Exception {
    MongoURI uri = new MongoURI("mongodb://usuario:usuario@ds053188.mlab.com:53188/soluciones-moviles");
    return new SimpleMongoDbFactory(uri);
}

From source file:TweetAnalytics.DBManager.java

public DBManager(String conStr) {

    // create the database
    try {/*  w  w w  .  ja  va 2  s .  com*/
        // make the initial connection to the mongoDB
        @SuppressWarnings("deprecation")
        Mongo tweetsMongoClient = new Mongo(new MongoURI(conStr));
        db = tweetsMongoClient.getDB("twitter_mini");
    } catch (UnknownHostException ex) {
        System.err.println("The database could not be initialized because of an UnknownHostException.");
        Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:TweetCollector.DBManager.java

@SuppressWarnings("deprecation")
public DBManager(String conStr) {

    // create the database
    try {//from   w  w w .ja  v  a  2 s . com
        // make the initial connection to the mongoDB
        Mongo tweetsMongoClient = new Mongo(new MongoURI(conStr));
        DBManager.db = tweetsMongoClient.getDB("twitter_mini");
    } catch (UnknownHostException ex) {
        System.err.println("The database could not be initialized because of an UnknownHostException.");
        Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
    }

    // create the tweets collection
    DBManager.tweetsCollection = DBManager.db.getCollection("tweets");
    // tweetsCollection.ensureIndex(new BasicDBObject("text", "text"));

    // create the trends collection
    DBManager.trendsCollection = DBManager.db.getCollection("trends");
}

From source file:v7db.files.Configuration.java

License:Open Source License

public static final Mongo getMongo(Properties props) throws UnknownHostException, MongoException {
    MongoURI uri = new MongoURI(props.getProperty("db.uri"));
    Mongo mongo = new Mongo(uri);
    return mongo;
}

From source file:v7views.mongo.InitMongo.java

License:Open Source License

public static V7Collection getCollection(ServletContext context, String name) {
    BSONBackedObject conf = getConfig(context);
    Object data = conf.getField("collections." + name);
    if (data instanceof String) {
        MongoURI uri = new MongoURI((String) data);
        return new V7Collection(getMongo(context).getDB(uri.getDatabase()).getCollection(uri.getCollection()));
    }/*from   w w w.j ava2  s .c  o m*/
    BSONBackedObject b = (BSONBackedObject) data;
    V7Collection base = getCollection(context, b.getStringField("collection"));
    return new V7Collection(base, b.getObjectField("filter"));
}