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.deafgoat.ml.prognosticator.MongoMgr.java

License:Apache License

/**
 * @param host/*  w w  w  . j  a  va2  s . co m*/
 *            The hostname of the database
 * @param port
 *            The port number of the database
 * @throws UnknownHostException
 *             If the database host cannot be resolved
 * @throws MongoException
 *             If it is unable to connect to a running mongod
 */
protected void setMongo(String host, Integer port) throws UnknownHostException, MongoException {
    _mongo = new Mongo(host, port);
}

From source file:com.dentinium.db.dbconn.java

public void openDB() throws MongoException, IOException {
    // connect to mongoDB, ip and port number
    mongo = new Mongo("localhost", 27017);

    // get database from MongoDB,
    // if database doesn't exists, mongoDB will create it automatically
    db = mongo.getDB("dedikodu");
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * Instantiates a new news handler.//  w  ww  .j a va 2s  .  c o m
 *
 * @param host
 *            Host the mongodb is running
 * @param port
 *            Port of the mongodb
 * @param db
 *            The name of the mongodb
 * @throws Exception
 *             Exception will thrown an database errors
 * @param webpath
 *            the webpath
 */
public NewsHandler(String host, int port, String db, String webpath) throws Exception {
    this.webpath = webpath;

    m = new Mongo(host, port);
    mdb = m.getDB(db);
    dbNews = mdb.getCollection(NEWS_COLLECTION_NAME);
    dbCategories = mdb.getCollection(CATEGORY_COLLECTION_NAME);
    dbMetaCategories = mdb.getCollection(META_CATEGORY_COLLECTION_NAME);
}

From source file:com.espirit.moddev.examples.uxbridge.newswidget.mongodb.ArticleHandler.java

License:Apache License

/**
 * Instantiates a new news handler./*from w w w.  ja  v  a  2 s  . c  om*/
 *
 * @param host          Host the mongodb is running
 * @param port          Port of the mongodb
 * @param db            The name of the mongodb
 * @throws Exception Exception will thrown an database errors
 */
public ArticleHandler(String host, int port, String db) throws Exception {
    m = new Mongo(host, port);
    mdb = m.getDB(db);
    articles = mdb.getCollection(COLLECTION_NAME);
}

From source file:com.fuction.MongoDB.java

public static void input(ArrayList<String> lsResult) {
    try {//ww  w .  j  a  v a2  s .c o  m
        Mongo mongo = new Mongo(HOST, PORT);
        DB db = mongo.getDB(DB);
        DBCollection collection = db.getCollection("Data");
        for (String s : lsResult) {
            DBObject dbObject = (DBObject) JSON.parse(s);
            collection.insert(dbObject);
        }
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:com.fuction.MongoDB.java

public static void display() {
    try {//from   ww w  .ja v a2  s .c  om
        Mongo mongo = new Mongo(HOST, PORT);
        DB db = mongo.getDB(DB);
        DBCollection collection = db.getCollection("Data");
        System.out.println("Collection mycol selected successfully");
        DBCursor cursor = collection.find();
        int i = 1;
        while (cursor.hasNext()) {
            System.out.println("Inserted Document: " + i);
            String jsonString = cursor.next().toString();
            System.out.println(jsonString);
            JSONObject jsonObject = parseJSONObject(jsonString);
            System.out.println(jsonObject.get("Area"));
            i++;
        }
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        e.printStackTrace();
    }
}

From source file:com.fuction.MongoDB.java

public static ArrayList<InformationObj> getObj() {
    ArrayList<InformationObj> list = new ArrayList<>();
    try {//from w  ww  .  j  a  v a  2  s  .c o  m
        Mongo mongo = new Mongo(HOST, PORT);
        DB db = mongo.getDB(DB);
        DBCollection collection = db.getCollection("Data");
        System.out.println("Collection mycol selected successfully");
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            String jsonString = cursor.next().toString();
            //System.out.println(jsonString);
            JSONObject jsonObject = parseJSONObject(jsonString);

            String roomNumber = (String) jsonObject.get("RoomNumber");
            String area = (String) jsonObject.get("Area");
            String address = (String) jsonObject.get("Address");
            String direction = (String) jsonObject.get("Direction");
            String numberOfBedRooms = (String) jsonObject.get("NumberOfBedRooms");
            String numberOfBathRooms = (String) jsonObject.get("NumberOfBathRooms");
            String project = (String) jsonObject.get("Project");
            String floor = (String) jsonObject.get("Floor");
            String utilities = (String) jsonObject.get("Utilities");
            String environment = (String) jsonObject.get("Environment");
            String description = (String) jsonObject.get("Description");
            String pricePerMetreSquare = (String) jsonObject.get("PricePerMetreSquare");
            String price = (String) jsonObject.get("Price");
            String image = (String) jsonObject.get("Image");
            String city = (String) jsonObject.get("City");
            String district = (String) jsonObject.get("District");

            InformationObj iObj = new InformationObj(roomNumber, area, address, direction, numberOfBedRooms,
                    numberOfBathRooms, project, floor, utilities, environment, description, pricePerMetreSquare,
                    price, image, city, district);
            list.add(iObj);
        }
    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        e.printStackTrace();
    }
    return list;
}

From source file:com.genius.admin.helper.MongoFactoryBean.java

License:Apache License

@Override
protected Mongo createInstance() throws Exception {
    if (replicaSetSeeds.size() > 0) {
        if (mongoOptions != null) {
            return new Mongo(replicaSetSeeds, mongoOptions);
        }/*from ww  w . j  av a  2s.c o m*/
        return new Mongo(replicaSetSeeds);
    }
    return new Mongo();
}

From source file:com.github.carlomicieli.nerdmovies.config.ComponentTestConfig.java

License:Apache License

/**
 * Return the mongodb factory for the test environment.
 *
 * @return the mongodb factory bean.// w ww. jav  a 2 s.c o m
 * @throws Exception
 */
@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
    return new SimpleMongoDbFactory(
            new Mongo(env.getProperty("mongo.hostName"), env.getProperty("mongo.portNumber", Integer.class)),
            env.getProperty("mongo.databaseName"));
}

From source file:com.github.niltz.maven.plugins.mongodb.AbstractMongoDBMojo.java

License:Open Source License

/**
 * Opens a connection using the given settings.
 * /*from w w  w  . j  a  va2s . co  m*/
 * @param dbSettings
 *            the connection settings
 * @return the Connection
 * @throws MojoFailureException
 *             on error
 * @throws UnknownHostException
 */
protected Mongo openConnection(ConnectionSettings connectionSettings)
        throws MojoFailureException, UnknownHostException {

    // get server address
    ServerAddress serverAddr = (connectionSettings.getPort() != null)
            ? new ServerAddress(connectionSettings.getHostname(), connectionSettings.getPort().intValue())
            : new ServerAddress(connectionSettings.getHostname());

    // get Mongo
    Mongo mongo = (connectionSettings.getOptions() != null)
            ? new Mongo(serverAddr, connectionSettings.getOptions())
            : new Mongo(serverAddr);

    // we're good :)
    return mongo;
}