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.biz.mongo.MongoDBConnection.java

public MongoDBConnection() {

    try {//from  w ww.j a va 2s . c om
        mongo = new Mongo("localhost", 27017);
        db = mongo.getDB("dialogservice");
        context = db.getCollection("context");
        System.out.println("Return collection object.......");

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.card.loop.xyz.config.DatabaseManager.java

public static MongoOperations getMongoOpsInstance(String databaseName) throws UnknownHostException {
    if (instance == null) {
        instanceMongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port);
        instance = new MongoTemplate(instanceMongo, databaseName);
    }//from www. j av  a2s .c o  m
    return instance;
}

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   ww w  . j a v  a  2 s.c om*/

    // 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();//ww  w.j av a 2  s  . 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 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  a v  a  2  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());/*  ww  w .ja v  a 2  s. com*/
    }
    return list;
}

From source file:com.cubeia.games.poker.common.mongo.MongoStorage.java

License:Open Source License

private void connectToMongo() throws UnknownHostException {
    morphia = new Morphia();
    morphia.getMapper().getConverters().addConverter(BigDecimalConverter.class);
    datastore = morphia.createDatastore(new Mongo(host.trim(), port), databaseName);
}

From source file:com.cubeia.poker.tournament.history.storage.impl.DatabaseStorageService.java

License:Open Source License

@Override
public void init(ServiceContext context) throws SystemException {
    DatabaseStorageConfiguration configuration = getConfiguration(context);
    try {/*  w ww  . j  a  va 2  s.  c  om*/
        Mongo mongo = new Mongo(configuration.getHost(), configuration.getPort());

        Morphia morphia = new Morphia();
        morphia.getMapper().getConverters().addConverter(BigDecimalConverter.class);
        dao = new HistoricTournamentDao(morphia.createDatastore(mongo, configuration.getDatabaseName()));
    } catch (UnknownHostException e) {
        throw new SystemException("Failed initializing datasource", e);
    }
}

From source file:com.cyslab.craftvm.rest.mongo.MongoDB.java

License:GNU General Public License

public static Mongo get() {

    if (db == null) {
        try {/*from   ww  w . j  a v  a  2 s.  co m*/
            List<ServerAddress> addrs = str2addresses(Config.mongo_servers);
            MongoOptions opts = new MongoOptions();
            opts.autoConnectRetry = true;
            int thrno = Config.server_threadsno;
            if (thrno < 100)
                opts.connectionsPerHost = thrno;
            else
                opts.connectionsPerHost = 100;
            opts.threadsAllowedToBlockForConnectionMultiplier = 10;
            opts.maxWaitTime = 10000; // millisecs
            db = new Mongo(addrs, opts);
            write_concern = Config.mongo_safeoperations ? WriteConcern.FSYNC_SAFE : WriteConcern.NORMAL;
            log.info("getDB():" + db);
        } catch (UnknownHostException e) {
            log.error("Bad host " + e);
            db = null;
        }
    }

    return db;

}