Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

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

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:com.aw.app.action.UserAction.java

/**
 * //from   w  w w  . jav  a 2s.  c o m
 * @param uid
 * @param type
 * @return 
 */
public long getUserResource(Object uid, String type) {
    DBObject user = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_user")
            .findOne(new BasicDBObject().append("uid", uid));
    long amount = 0;
    if (user != null) {
        String tid = "";
        String maxField = "";
        String townHallStorageField = "";

        if ("spens".equalsIgnoreCase(type)) {
            return (Long) user.get("spens");
        } else if ("gold".equalsIgnoreCase(type)) {
            tid = BuildingAction.resourceStorage[0];
            maxField = "max_stored_gold";
            townHallStorageField = "gold_stored";

        } else if ("mithril".equalsIgnoreCase(type)) {
            tid = BuildingAction.resourceStorage[1];
            maxField = "max_stored_mithril";
            townHallStorageField = "mithril_stored";
        } else if ("dark mithril".equalsIgnoreCase(type)) {
            tid = BuildingAction.resourceStorage[2];
            maxField = "max_stored_dark_mithril";
            townHallStorageField = "dark_mithril_stored";
        }

        DBCursor cursor = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base")
                .find(new BasicDBObject().append("uid", uid).append("tid", tid));
        DBObject townHallBase = MongoDbUtil.getCollection(MongoDbUtil.defaultDBName, "aw_building_base")
                .findOne(new BasicDBObject().append("uid", uid).append("tid", TownHallAction.townHallTid));
        BuildingTownhall townHall = ((List<BuildingTownhall>) townHallBase.get("buildingTownhalls")).get(0);
        amount = townHall.getLong(townHallStorageField);
        while (cursor.hasNext()) {
            DBObject building = cursor.next();
            BuildingResource resource = ((List<BuildingResource>) building.get("buildingResources")).get(0);
            amount += resource.getLong("resource_stored");
        }
    }
    return amount;
}

From source file:com.aw.services.AncientWarServiceImpl.java

/**
 * //from ww  w  .  j  a  v a2  s  . c om
 * @param userId
 * @return
 * @throws JSONException 
 */
@Override
public JSONObject browseUserVillage(Long userId) throws JSONException {
    DBCollection coll = MongoDbUtil.getCollection("ancient_war", "aw_user");
    DBObject query = QueryBuilder.start("uid").is(userId).get();
    DBCursor cursor = coll.find(query);
    JSONObject status = new JSONObject();
    while (cursor.hasNext()) {
        DBObject user = cursor.next();
        if (user != null) {
            Long uid = (Long) user.get("uid");
            JSONObject userVillage = getUserVillage(uid, "visit");
            return userVillage;
        } else {
            status.put("status", false);
            status.put("message", "No Village found");
            return status;
        }
    }
    return status;
}

From source file:com.aw.services.AncientWarServiceImpl.java

/**
 * //from w  w w  . j  ava2  s .  c o  m
 * @param key
 * @return
 * @throws JSONException 
 */
@Override
public JSONObject getSystemSettingByKey(String key) throws JSONException {
    DBCollection collection = MongoDbUtil.getCollection("ancient_war", "aw_system_settings");
    DBObject dbObject = new BasicDBObject();
    dbObject.put("key", key);
    DBObject filteredObject = collection.findOne(dbObject);
    JSONObject json = new JSONObject();
    if (filteredObject != null) {
        json.put("status", filteredObject.get("value").toString());
    } else {
        json.put("status", "");
    }
    return json;
}

From source file:com.aw.util.MongoDbUtil.java

public static Object findMaxValueForKey(String collection, String key) {
    DBCollection table = getCollection(defaultDBName, collection);
    BasicDBObject query = new BasicDBObject();
    query.put(key, -1);/*from w ww .  j  av  a 2s . co m*/
    DBCursor cursor = table.find().sort(query).limit(1);
    Object value = null;
    while (cursor.hasNext()) {
        DBObject object = cursor.next();
        value = object.get(key);
    }
    return value;
}

From source file:com.azure.api.MongoDAO.java

public static LinkedList<Pojoo> MongoConnection() throws IOException {

    MongoClient mongo = new MongoClient("localhost", 27017);

    DB db = mongo.getDB("test");

    DBCollection table = db.getCollection("DataFinal");

    List<String> uniId = table.distinct("Asin");

    //BasicDBObject searchQuery = new BasicDBObject();
    //searchQuery.put("Asin", "000100039X");
    DBObject groupFields = new BasicDBObject("_id", "$Asin");

    groupFields.put("count", new BasicDBObject("$sum", 1));
    //DBObject group = new BasicDBObject("$group", groupFields );
    DBObject sortFields = new BasicDBObject("count", -1);
    //AggregationOutput output = table.aggregate(group, new BasicDBObject("$sort", sortFields ));
    //System.out.println(output.results());//Top reviews 

    groupFields.put("average", new BasicDBObject("$avg", "$Ratings"));
    DBObject group2 = new BasicDBObject("$group", groupFields);

    AggregationOutput output2 = table.aggregate(group2, new BasicDBObject("$sort", sortFields));

    Iterable<DBObject> iterable = output2.results();

    //LinkedList<String>stringResult = new LinkedList<String>();

    //LinkedList<String>stringrating = new LinkedList<String>();

    //if(awsResult.size()!=0){awsResult.remove();}
    //if(stringResult.size()!=0){stringResult.remove();}

    //System.out.println("fffffffffffffffff"+stringResult.size());

    int count = 0;

    LinkedList<Pojoo> uiList = new LinkedList<Pojoo>();
    for (DBObject res : output2.results()) {

        //System.out.println("TTTTTTTT "+res.get("_id").toString());
        //System.out.println("VVVVVVVV "+res.get("average").toString());
        String awsResult = awsApi.getDetails(res.get("_id").toString());
        //stringResult.add(res.get("_id").toString());
        //stringrating.add(res.get("average").toString());
        Pojoo polo = new Pojoo();
        polo.setStringResult(awsResult);
        polo.setStringrating(res.get("average").toString());
        uiList.add(polo);//ww w . j  a  va 2 s . c  o m

        count++;
        if (count == 8) {
            res = null;
            break;
        }
    }

    count = 0;

    //awsResult = awsApi.getDetails(stringResult);
    //for(String htp: awsResult){
    //Pojoo polo = new Pojoo();
    //polo.setStringResult(htp);
    //polo.setStringrating(res.get("average").toString());
    //uiList.add(polo);
    //}

    // System.out.println("WWWWWW"+awsResult);
    //Pojoo img = new Pojoo();
    //img.setStringrating(stringrating);
    //img.setStringResult(awsResult);
    //stringResult.remove();

    //System.out.println("eeeeeeeeeeeeeee"+awsResult.size());
    //for(String ret:awsResult)
    //{
    //   System.out.println(ret);
    //}

    return uiList;

}

From source file:com.azure.api.MongoDAO.java

public static LinkedList<String> MongoConnection2() throws IOException {

    MongoClient mongo = new MongoClient("localhost", 27017);

    DB db = mongo.getDB("test");

    DBCollection table = db.getCollection("DataFinal");
    DBObject sortFields = new BasicDBObject("count", -1);
    DBObject groupGenreFields = new BasicDBObject("_id", "$Genre1");
    groupGenreFields.put("count", new BasicDBObject("$sum", 1));
    DBObject genregroup = new BasicDBObject("$group", groupGenreFields);
    AggregationOutput Genreoutput = table.aggregate(genregroup, new BasicDBObject("$sort", sortFields));

    LinkedList<String> stringResult = new LinkedList<String>();

    int count = 0;
    for (DBObject res : Genreoutput.results()) {
        stringResult.add(res.get("_id").toString());
        System.out.println(res.get("_id").toString());
        count++;// w w w  .  j  a  v  a2  s. co m
        if (count == 8)
            break;

    }

    System.out.print("strinResult Size" + stringResult.size());

    return stringResult;

}

From source file:com.azure.api.MongoDAO.java

public static LinkedList<String> MongoConnection3() throws IOException {

    MongoClient mongo = new MongoClient("localhost", 27017);

    DB db = mongo.getDB("test");

    DBCollection table = db.getCollection("DataFinal");
    DBObject sortFields = new BasicDBObject("count", -1);
    DBObject groupGenreFields = new BasicDBObject("_id", "$Author");
    groupGenreFields.put("count", new BasicDBObject("$sum", 1));
    DBObject genregroup = new BasicDBObject("$group", groupGenreFields);
    AggregationOutput Genreoutput = table.aggregate(genregroup, new BasicDBObject("$sort", sortFields));

    LinkedList<String> stringResult = new LinkedList<String>();

    int count = 0;
    for (DBObject res : Genreoutput.results()) {
        stringResult.add(res.get("_id").toString());
        System.out.println(res.get("_id").toString());
        count++;//  w  ww .  j av  a2s  .co m
        if (count == 8)
            break;

    }

    System.out.print("strinResult Size" + stringResult.size());

    return stringResult;

}

From source file:com.badger.observationmongotest.execute.Main.java

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

    List<Observation> observations = new LinkedList();

    Customer customer = new Customer();

    customer.setName("Mr Customer");
    customer.setId("cid123");
    customer.setBackOfficeId("boid123");

    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
    String dateStr = "2015/08/10";
    Date date = format.parse(dateStr);

    CustomerObservationReport report = new CustomerObservationReport(customer);

    MonthlyProductOrderCountIssue productOrderCountIssue = new MonthlyProductOrderCountIssue();
    productOrderCountIssue.setActionRequired(true);
    productOrderCountIssue.setCatagory("product issue");
    productOrderCountIssue.setCurrentYear(2015);
    productOrderCountIssue.setCurrentYearValue(30);
    productOrderCountIssue.setCustomerId(customer.getId());
    productOrderCountIssue.setDate(date);
    productOrderCountIssue.setDrop(50.0);
    productOrderCountIssue.setLastYearValue(60);
    productOrderCountIssue.setMonth("Feb");
    productOrderCountIssue.setObservationId("oid123");
    productOrderCountIssue.setPreviousYear(2014);
    productOrderCountIssue.setProductName("cool product");
    productOrderCountIssue.setScore(4500);
    productOrderCountIssue.setType("monthy order count");

    MonthlyProductOrderCountIssue monthlyProductOrderCountIssue = new MonthlyProductOrderCountIssue();
    monthlyProductOrderCountIssue.setActionRequired(true);
    monthlyProductOrderCountIssue.setCatagory("product issue");
    monthlyProductOrderCountIssue.setCurrentYear(2015);
    monthlyProductOrderCountIssue.setCurrentYearValue(30);
    monthlyProductOrderCountIssue.setCustomerId(customer.getId());
    monthlyProductOrderCountIssue.setDate(date);
    monthlyProductOrderCountIssue.setDrop(50.0);
    monthlyProductOrderCountIssue.setLastYearValue(60);
    monthlyProductOrderCountIssue.setMonth("March");
    monthlyProductOrderCountIssue.setObservationId("oid456");
    monthlyProductOrderCountIssue.setPreviousYear(2014);
    monthlyProductOrderCountIssue.setProductName("cool product");
    monthlyProductOrderCountIssue.setScore(4500);
    monthlyProductOrderCountIssue.setType("monthy order count");

    MonthlyProductRevenueIssue monthlyProductRevenueIssue = new MonthlyProductRevenueIssue();
    monthlyProductRevenueIssue.setActionRequired(true);
    monthlyProductRevenueIssue.setCatagory("product issue");
    monthlyProductRevenueIssue.setCurrentYear(2015);
    monthlyProductRevenueIssue.setCurrentYearValue(3000.00);
    monthlyProductRevenueIssue.setCustomerId(customer.getId());
    monthlyProductRevenueIssue.setDate(date);
    monthlyProductRevenueIssue.setDrop(50.0);
    monthlyProductRevenueIssue.setLastYearValue(6000.00);
    monthlyProductRevenueIssue.setMonth("Dec");
    monthlyProductRevenueIssue.setObservationId("oid3445");
    monthlyProductRevenueIssue.setPreviousYear(2014);
    monthlyProductRevenueIssue.setProductName("test product");
    monthlyProductRevenueIssue.setScore(5667);
    monthlyProductRevenueIssue.setType("monthly product ervenue");

    MonthlyRevenueIssue monthlyRevenueIssue = new MonthlyRevenueIssue();
    monthlyRevenueIssue.setActionRequired(true);
    monthlyRevenueIssue.setCatagory("Reveue Issue");
    monthlyRevenueIssue.setCurrentValue(3000);
    monthlyRevenueIssue.setCurrentYear(2015);
    monthlyRevenueIssue.setCustomerId(customer.getId());
    monthlyRevenueIssue.setDate(date);//from   w  w w .j  a  v a  2s  . c om
    monthlyRevenueIssue.setDrop(45.0);
    monthlyRevenueIssue.setMonth("Nov");
    monthlyRevenueIssue.setObservationId("oid4646646");
    monthlyRevenueIssue.setPreviousValue(5000);
    monthlyRevenueIssue.setPreviousYear(2014);
    monthlyRevenueIssue.setScore(5600);
    monthlyRevenueIssue.setType("monthly revenue issue");

    observations.add(monthlyRevenueIssue);
    observations.add(monthlyProductRevenueIssue);
    observations.add(monthlyProductOrderCountIssue);
    observations.add(productOrderCountIssue);

    report.setAccountId("acid123");

    report.addObservations(observations);

    // BasicDBObject documentDetail = new BasicDBObject();
    /**
     * documentDetail.put("accountId", report.getAccountId());
     * documentDetail.put("customerId", report.getCustomerId());
     * documentDetail.put("Observations",
     * report.getObservations().toString()); *
     */
    MongoClient mongo = new MongoClient("localhost", 27017);
    DB db = mongo.getDB("ObservationReports");

    DBCollection col = db.getCollection("reports");

    Gson gson = new Gson();

    String json = gson.toJson(report);

    DBObject dbObject = (DBObject) JSON.parse(json);

    col.insert(dbObject);

    //DBObject query = BasicDBObjectBuilder.start().add("_id", report.getId()).get();
    DBCursor cursor = col.find();

    while (cursor.hasNext()) {
        System.out.println("First Cursor: " + cursor.next().get("observations"));
    }

    while (cursor.hasNext()) {

        DBObject dbobj = cursor.next();

        Map map = dbObject.toMap();

        CustomerObservationReport report2 = new CustomerObservationReport(customer);

        //System.out.println("observations " +dbObject.get("observations"));

        List<Observation> dbObservations = (List<Observation>) dbObject.get("observations");

        report2.setAccountId(report.getAccountId());
        report2.setCustomerId(report.getCustomerName());
        report2.setBackOfficeId(report.getBackOfficeId());
        report2.setObservations((dbObservations));

        System.out.println(report2.getClass().getTypeName());
    }
}

From source file:com.bbc.remarc.ws.MetadataService.java

License:Apache License

/**
 * Determine if db object already exists in the provided collection. 
 * @param newObj object to check //w  w w . java2 s. c om
 * @param results collection to determine if newObj exists
 * @return true if it does false if not.
 */
private boolean resultsContains(DBObject newObj, List<DBObject> results) {

    boolean result = false;

    for (DBObject obj : results) {
        if (obj.get("id").equals(newObj.get("id"))) {
            return true;
        }
    }

    return result;
}

From source file:com.beans.Notifications.java

public static Notifications fromDBObject(DBObject dbo) {
    Notifications n = new Notifications();
    n.sender = (String) dbo.get("sender");
    n.receiver = (String) dbo.get("receiver");
    n.message = (String) dbo.get("message");
    n.read = (Boolean) dbo.get("read");
    n.url = (String) dbo.get("url");
    n.claimId = (Integer) dbo.get("claimid");

    return n;/*from  w w  w  .  j a v  a 2 s.co  m*/
}