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:ch.agent.crnickl.mongodb.MongoDB.java

License:Apache License

private void initialize(Mongo mongo, Database database) throws T2DBException {
    DB db = mongo.getDB(configuration.getParameter(MONGODB_DB, true));
    db.setWriteConcern(getWriteConcernFromKeyword(configuration.getParameter(MONGODB_WRITE_CONCERN, false)));
    if (!db.collectionExists(MongoDatabase.COLL_VT)) {
        valueTypes = createCollection(db, MongoDatabase.COLL_VT, MongoDatabase.FLD_VT_NAME);
        properties = createCollection(db, MongoDatabase.COLL_PROP, MongoDatabase.FLD_PROP_NAME);
        schemas = createCollection(db, MongoDatabase.COLL_SCHEMA, MongoDatabase.FLD_SCHEMA_NAME);
        chronicles = createCollection(db, MongoDatabase.COLL_CHRON, MongoDatabase.FLD_CHRON_PARENT,
                MongoDatabase.FLD_CHRON_NAME);
        series = createCollection(db, MongoDatabase.COLL_SER, MongoDatabase.FLD_SER_CHRON,
                MongoDatabase.FLD_SER_NUM);
        /* can't index on variable keys ... this is yet another NO GO for MONGO)
        createIndex(series, MongoDatabase.FLD_SER_CHRON, MongoDatabase.FLD_SER_NUM, 
              MongoDatabase.FLD_SER_VALUES ... errr); */
        attributes = createCollection(db, MongoDatabase.COLL_ATTR, MongoDatabase.FLD_ATTR_CHRON,
                MongoDatabase.FLD_ATTR_PROP);
        createIndex(attributes, MongoDatabase.FLD_ATTR_PROP, MongoDatabase.FLD_ATTR_VALUE);
        createBuiltInValueTypes(database);
    } else {//w  w w .  j  a v a 2 s . co  m
        valueTypes = db.getCollection(MongoDatabase.COLL_VT);
        properties = db.getCollection(MongoDatabase.COLL_PROP);
        schemas = db.getCollection(MongoDatabase.COLL_SCHEMA);
        chronicles = db.getCollection(MongoDatabase.COLL_CHRON);
        series = db.getCollection(MongoDatabase.COLL_SER);
        attributes = db.getCollection(MongoDatabase.COLL_ATTR);
    }
}

From source file:Collector.MongoDbConnector.java

public static void initMongoDB() throws MongoException {
    try {//from  w  w  w.j av a 2  s. co  m
        System.out.println("Connecting to Mongo Db..");
        Mongo mongo;
        mongo = new Mongo("127.0.0.1");
        db = mongo.getDB("tweetDB");

    } catch (UnknownHostException ex) {
        System.out.println("MongoDb Connection Erorr :" + ex.getMessage());
    }
}

From source file:com.AlertMailerWebPage.servlet.Show.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w ww  . ja  v a2  s.  c  o  m
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        Properties defaultProps = new Properties();
        InputStream in = GetProcessSpeed.class.getResourceAsStream("configuration.properties");
        defaultProps.load(in);
        in.close();
        String database = (String) defaultProps.get("database_mailerDetails");
        String collection = (String) defaultProps.get("collection_mailerDetails");
        String serverAddress = (String) defaultProps.get("IP_AMDBoard");

        Mongo mongo = new Mongo("localhost", 27017);
        //   Mongo mongo=new Mongo(serverAddress,27017);
        //DB db = mongo.getDB("dashboard" );
        DB db = mongo.getDB(database);

        DBCollection coll = db.getCollection(collection);
        //DBCollection coll=db.getCollection("users");
        DBObject query = new BasicDBObject("AlertMailer", "PRS");
        DBObject query1 = new BasicDBObject("InsertTime", -1);
        //DBObject query = new BasicDBObject("_id",-1);
        DBCursor cursor = coll.find(query).sort(query1).limit(1);

        //String name1= (String)cursor.one().get("name");    
        //while(cursor.hasNext()){
        //    out.println(""+cursor.next()+"");
        //}
        response.setContentType("text/plain");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(cursor.one().toString());
    }
}

From source file:com.arquivolivre.mongocom.management.CollectionManager.java

License:Apache License

protected CollectionManager(Mongo client, String dataBase) {
    this.client = client;
    if (dataBase != null && !dataBase.equals("")) {
        this.db = client.getDB(dataBase);
    } else {/*from   www  .j a va  2  s  . com*/
        this.db = client.getDB(client.getDatabaseNames().get(0));
    }
}

From source file:com.bbytes.zorba.domain.testing.EmbeddedMongoDBFactory.java

License:Apache License

@Override
public DB getDb(String dbName) throws DataAccessException {
    //return if db already exists
    if (db != null && db.getName().equals(dbName)) {
        return db;
    }//from  w w w. ja v a 2s  .co m
    try {
        int port = 12345;
        MongodConfig mongodConfig = new MongodConfig(Version.Main.V2_0, port, Network.localhostIsIPv6());
        MongodStarter runtime = MongodStarter.getDefaultInstance();
        mongodExecutable = runtime.prepare(mongodConfig);
        mongodExecutable.start();
        Mongo mongo = new Mongo("localhost", port);
        db = mongo.getDB(dbName);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return db;
}

From source file:com.card.loop.xyz.dao.LearningElementDAO.java

public boolean addFile(LearningElement le) throws UnknownHostException, IOException {
    File file = new File(AppConfig.USER_VARIABLE + le.getFilePath() + le.getFilename());
    Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port);
    DB db = mongo.getDB(AppConfig.DATABASE_LOOP);

    GridFS gf = new GridFS(db, "le.meta");
    GridFSInputFile gfsFile = gf.createFile(file);
    gfsFile.setFilename(le.getFilename());
    gfsFile.setContentType(le.getContentType());
    gfsFile.put("_class", "com.card.loop.xyz.model.LearningElement");
    gfsFile.put("title", le.getTitle());
    gfsFile.put("filePath", le.getFilePath());
    gfsFile.put("subject", le.getSubject());
    gfsFile.put("description", le.getDescription());
    gfsFile.put("downloads", le.getDownloads());
    gfsFile.put("rating", le.getRating());
    gfsFile.put("comments", le.getComments());
    gfsFile.put("uploadedBy", le.getUploadedBy());
    gfsFile.put("status", le.getStatus());
    gfsFile.put("rev", le.getRev());
    gfsFile.put("type", le.getType());
    gfsFile.save();/*from  w  w w. j  ava2s .  c o  m*/

    // Let's store our document to MongoDB
    /*   System.out.println("SEARCH: " + search(gfsFile.getMD5(), "le.meta"));
       if(search(gfsFile.getMD5(), "le.meta") > 1){            
    deleteLE(le.getFileName(),"le.meta");
       }*/
    //
    //   collection.insert(info, WriteConcern.SAFE);
    return true;
}

From source file:com.card.loop.xyz.dao.LearningElementDAO.java

public boolean addFile(LearningElement le, File file) throws UnknownHostException, IOException {
    Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port);
    DB db = mongo.getDB(AppConfig.DATABASE_LOOP);

    GridFS gf = new GridFS(db, "le.meta");
    GridFSInputFile gfsFile = gf.createFile(file);
    gfsFile.setFilename(le.getFilename());
    gfsFile.setContentType(le.getContentType());
    gfsFile.put("_class", "com.card.loop.xyz.model.LearningElement");
    gfsFile.put("title", le.getTitle());
    gfsFile.put("filePath", le.getFilePath());
    gfsFile.put("subject", le.getSubject());
    gfsFile.put("description", le.getDescription());
    gfsFile.put("downloads", le.getDownloads());
    gfsFile.put("rating", le.getRating());
    gfsFile.put("comments", le.getComments());
    gfsFile.put("uploadedBy", le.getUploadedBy());
    gfsFile.put("status", le.getStatus());
    gfsFile.put("rev", le.getRev());
    gfsFile.put("type", le.getType());
    gfsFile.save();//w  w  w. ja v a 2  s.co m

    // Let's store our document to MongoDB
    /*   System.out.println("SEARCH: " + search(gfsFile.getMD5(), "le.meta"));
       if(search(gfsFile.getMD5(), "le.meta") > 1){            
    deleteLE(le.getFileName(),"le.meta");
       }*/
    //
    //   collection.insert(info, WriteConcern.SAFE);
    return true;
}

From source file:com.card.loop.xyz.dao.LearningElementDAO.java

public ArrayList<DBObject> listAll(String collection) throws UnknownHostException {
    ArrayList<DBObject> list = new ArrayList<DBObject>();
    Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port);
    DB db = mongo.getDB(AppConfig.DATABASE_LOOP);
    GridFS le_gfs = new GridFS(db, collection);
    DBCursor cursor = le_gfs.getFileList();
    System.out.println(le_gfs.getFileList() + "");
    while (cursor.hasNext()) {
        list.add(cursor.next());/*  w w  w  .  j av  a2 s  .co  m*/
    }
    return list;
}

From source file:com.card.loop.xyz.dao.LearningElementDAO.java

public GridFSDBFile getSingleLE(String id, String collection) throws UnknownHostException {
    Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port);
    DB db = mongo.getDB(AppConfig.DATABASE_LOOP);
    GridFS le_gfs = new GridFS(db, collection);
    // GridFSDBFile le_output = le_gfs.findOne(new ObjectId(id));
    GridFSDBFile le_output = le_gfs.findOne(new ObjectId(id));
    System.out.println(le_output);
    return le_output;
}

From source file:com.card.loop.xyz.dao.LearningElementDAO.java

public ArrayList<String> getKeywords(String md5, String collection) throws UnknownHostException {
    Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port);
    DB db = mongo.getDB(AppConfig.DATABASE_LOOP);
    ArrayList<String> list = new ArrayList<>();
    GridFS le_gfs = new GridFS(db, collection);
    GridFSDBFile le_output = le_gfs.findOne(new BasicDBObject("md5", md5));
    ListIterator<Object> trustedList = ((BasicDBList) le_output.get("keywords")).listIterator();

    while (trustedList.hasNext()) {
        Object nextItem = trustedList.next();
        list.add(nextItem.toString());//from   ww w.ja v  a 2s  . c om
    }
    return list;
}