Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:edu.uniandes.yelp.recommender.MongoDBDataModel.java

private void buildModel() throws UnknownHostException {
    userIsObject = false;//from w  w w.  j av  a  2s.  c  om
    itemIsObject = false;
    idCounter = 0;
    preferenceIsString = true;
    MongoClient mongoDDBB = new MongoClient(mongoHost, mongoPort);
    DB db = mongoDDBB.getDB(mongoDB);
    mongoTimestamp = new Date(0);
    FastByIDMap<Collection<Preference>> userIDPrefMap = new FastByIDMap<>();
    if (!mongoAuth) {
        collection = db.getCollection(mongoCollection);
        collectionMap = db.getCollection(mongoMapCollection);
        DBObject indexObj = new BasicDBObject();
        indexObj.put("element_id", 1);
        collectionMap.createIndex(indexObj);
        indexObj = new BasicDBObject();
        indexObj.put("long_value", 1);
        collectionMap.createIndex(indexObj);
        collectionMap.remove(new BasicDBObject());
        DBCursor cursor = collection.find();
        while (cursor.hasNext()) {
            Map<String, Object> user = (Map<String, Object>) cursor.next().toMap();
            if (!user.containsKey("deleted_at")) {
                long userID = Long.parseLong(fromIdToLong(getID(user.get(mongoUserID), true), true));
                long itemID = Long.parseLong(fromIdToLong(getID(user.get(mongoItemID), false), false));
                float ratingValue = getPreference(user.get(mongoPreference));
                Collection<Preference> userPrefs = userIDPrefMap.get(userID);
                if (userPrefs == null) {
                    userPrefs = new ArrayList<>(2);
                    userIDPrefMap.put(userID, userPrefs);
                }
                userPrefs.add(new GenericPreference(userID, itemID, ratingValue));
                if (user.containsKey("created_at")
                        && mongoTimestamp.compareTo(getDate(user.get("created_at"))) < 0) {
                    mongoTimestamp = getDate(user.get("created_at"));
                }
            }
        }
    }
    delegate = new GenericDataModel(GenericDataModel.toDataMap(userIDPrefMap, true));
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void Insert(FlightInfo info, MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection flight = db.getCollection("flight");
    BasicDBObject query = new BasicDBObject("FL_DATE", info.getDate()).append("UNIQUE_CARRIER",
            info.getCarrier());/*from w w w  . j ava2s. com*/
    query.append("FL_NUM", info.getTailNum()).append("ORIGIN_CITY_NAME", info.getDepCity());
    query.append("DEST_CITY_NAME", info.getArrCity()).append("CRS_DEP_TIME", info.getCrsDepTime());
    query.append("CRS_ARR_TIME", info.getCrsArrTime()).append("DISTANCE", info.getDistance());
    query.append("CANCELLED", info.getCancel());
    if (info.getCancel() == 0) {
        query.append("DEP_TIME", info.getDepTime()).append("DEP_DELAY", info.getDepDelay());
        query.append("ARR_TIME", info.getArrTime()).append("ARR_DELAY", info.getArrDelay());
    }
    System.out.println(query);
    flight.insert(query);
    //mongoClient.close();

}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void Update(String id, FlightInfo newInfo, MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection flight = db.getCollection("flight");
    ObjectId oId = new ObjectId(id);
    BasicDBObject query0 = new BasicDBObject("_id", oId);
    BasicDBObject query = new BasicDBObject("FL_DATE", newInfo.getDate()).append("UNIQUE_CARRIER",
            newInfo.getCarrier());//w  w  w .j a  v  a  2 s  .  co m
    query.append("FL_NUM", newInfo.getTailNum()).append("ORIGIN_CITY_NAME", newInfo.getDepCity());
    query.append("DEST_CITY_NAME", newInfo.getArrCity()).append("CRS_DEP_TIME", newInfo.getCrsDepTime());
    query.append("CRS_ARR_TIME", newInfo.getCrsArrTime()).append("DISTANCE", newInfo.getDistance());
    query.append("CANCELLED", newInfo.getCancel());
    if (newInfo.getCancel() == 0) {
        query.append("DEP_TIME", newInfo.getDepTime()).append("DEP_DELAY", newInfo.getDepDelay());
        query.append("ARR_TIME", newInfo.getArrTime()).append("ARR_DELAY", newInfo.getArrDelay());
    }
    System.out.println(query0);
    System.out.println(query);
    flight.update(query0, query);
    //mongoClient.close();
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void Delete(String id, MongoClient mongoClient) {
    ObjectId oId = new ObjectId(id);
    BasicDBObject query = new BasicDBObject("_id", oId);
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection flight = db.getCollection("flight");
    System.out.println(query);/*w  ww  .  ja v a2  s.c  o  m*/
    flight.remove(query);
    //mongoClient.close();
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static DBCursor Search(SearchInfo sInfo, String colName, MongoClient mongoClient) {

    BasicDBObject query = new BasicDBObject();

    //Time Range//from  www. j a  v  a  2  s  .  co  m
    if (sInfo.getUpDate() != null && sInfo.getDownDate() != null) {
        String upDateStr = sInfo.getUpDate().toString();
        String downDateStr = sInfo.getDownDate().toString();
        DateTimeFormatter df = DateTimeFormat.forPattern("yyyy-MM-dd");
        LocalDate upDate = df.parseLocalDate(upDateStr);
        LocalDate downDate = df.parseLocalDate(downDateStr);
        ArrayList dates = new ArrayList();
        for (LocalDate date = downDate; date.isBefore(upDate.plusDays(1)); date = date.plusDays(1)) {
            dates.add(date.toString());
        }
        query.append("FL_DATE", new BasicDBObject("$in", dates));
    }
    //Carrier
    if (sInfo.getCarrier() != null) {
        query.append("UNIQUE_CARRIER", sInfo.getCarrier());
    }
    //TailNum
    if (sInfo.getTailNum() != 0) {
        query.append("FL_NUM", sInfo.getTailNum());
    }
    //DepCity
    if (sInfo.getDepCity() != null) {
        query.append("ORIGIN_CITY_NAME", sInfo.getDepCity());
    }
    //ArrCity
    if (sInfo.getArrCity() != null) {
        query.append("DEST_CITY_NAME", sInfo.getArrCity());
    }
    //DistanceRange
    query.append("DISTANCE", new BasicDBObject("$gt", sInfo.getDownDis()).append("$lt", sInfo.getUpDis()));
    //DepTimeRange
    query.append("CRS_DEP_TIME",
            new BasicDBObject("$gt", sInfo.getDownDepTime()).append("$lt", sInfo.getUpDepTime()));
    //ArrTimeRange
    query.append("CRS_ARR_TIME",
            new BasicDBObject("$gt", sInfo.getDownArrTime()).append("$lt", sInfo.getUpArrTime()));
    //Cancel
    if (sInfo.getCancel() != -1) {
        query.append("CANCELLED", sInfo.getCancel());
    }

    System.out.println(query);
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection flight = db.getCollection(colName);
    DBCursor cursor = flight.find(query);
    return cursor;
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static ArrayList MapReduce(String colName, String map, String reduce, String finalize,
        MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection temp = db.getCollection(colName);

    ArrayList result = new ArrayList();
    MapReduceCommand cmd = new MapReduceCommand(temp, map, reduce, null, MapReduceCommand.OutputType.INLINE,
            null);// www  .ja  v a2s  . c om

    if (finalize != null) {
        cmd.setFinalize(finalize);
    }

    MapReduceOutput out = temp.mapReduce(cmd);

    for (DBObject o : out.results()) {
        result.add(o.toString());
    }
    System.out.println("Done");
    //mongoClient.close();
    return result;
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void Save(DBObject doc, String tempColName, MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection temp = db.getCollection(tempColName);
    temp.save(doc);/* w ww  . ja va  2s . c o  m*/
    //mongoClient.close();
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void CreateCol(String tempColName, MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBObject options = BasicDBObjectBuilder.start().add("capped", true).add("size", 2000000000l).get();
    db.createCollection(tempColName, options);
    //mongoClient.close();
}

From source file:edu.wayne.cs.fms.controller.CRUD.java

public static void DropCol(String colName, MongoClient mongoClient) {
    //MongoClient mongoClient = Connector.connect("localhost", 27017);
    DB db = mongoClient.getDB("project");
    DBCollection myCollection = db.getCollection(colName);
    myCollection.drop();/* w w  w . j  ava  2 s  .c o  m*/
    //mongoClient.close();
}

From source file:embl.ebi.variation.eva.vcfdump.VariantExporterTestDB.java

License:Apache License

public static void cleanDBs() throws UnknownHostException {
    logger.info("Cleaning test DBs ...");
    MongoClient mongoClient = new MongoClient("localhost");
    List<String> dbs = Arrays.asList(TEST_DB_NAME, COW_TEST_DB_NAME);
    for (String dbName : dbs) {
        DB db = mongoClient.getDB(dbName);
        db.dropDatabase();/*from w  ww  . j a v a2s  . com*/
    }
    mongoClient.close();
}