Example usage for com.mongodb DBObject put

List of usage examples for com.mongodb DBObject put

Introduction

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

Prototype

Object put(String key, Object v);

Source Link

Document

Sets a name/value pair in this object.

Usage

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);// w  ww. j  ava  2s .  c  om

        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++;/*from ww w .  ja va 2 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++;/*www  .j a v  a2 s.c  o m*/
        if (count == 8)
            break;

    }

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

    return stringResult;

}

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

License:Apache License

/**
 * Core find method for retrieving metadata for 
 * media //  w  ww  .  j av a2 s.  c o  m
 * @param db mongodb connection
 * @param dbCollection collection name
 * @param decade decade (optional)
 * @param theme theme (optional)
 * @param limit limit (required)
 * @return results
 */
protected List<DBObject> findMetadata(DB db, final String dbCollection, String decade, String theme,
        Long limit) {
    DBObject searchFilter = null;

    if (limit == null) {
        throw new WebApplicationException(Response.status(HttpURLConnection.HTTP_BAD_REQUEST)
                .entity("limit parameter is mandatory").build());
    }

    List<DBObject> results = new ArrayList<DBObject>();

    try {

        db.requestStart();

        if (decade != null || theme != null) {
            searchFilter = new BasicDBObject();
        }

        if (decade != null) {
            searchFilter.put("decade", decade);
        }

        if (theme != null) {
            searchFilter.put("theme", theme);
        }

        results = findRandom(db, limit, searchFilter, dbCollection);
    } finally {
        db.requestDone();
    }

    return results;
}

From source file:com.berkgokden.mongodb.MongoMapStore.java

License:Open Source License

public void store(Object key, Object value) {
    DBObject dbo = converter.toDBObject(value);
    dbo.put("_id", key);
    coll.save(dbo);//from w w w  . j a v  a  2s  .  c om
}

From source file:com.berkgokden.mongodb.MongoMapStore.java

License:Open Source License

public void delete(Object key) {
    DBObject dbo = new BasicDBObject();
    dbo.put("_id", key);
    coll.remove(dbo);
}

From source file:com.berkgokden.mongodb.MongoMapStore.java

License:Open Source License

public Object load(Object key) {
    DBObject dbo = new BasicDBObject();
    dbo.put("_id", key);
    DBObject obj = coll.findOne(dbo);//from   w  w  w. j av a 2s.  c  o m
    if (obj == null)
        return null;

    try {
        Class clazz = Class.forName(obj.get("_class").toString());
        return converter.toObject(clazz, obj);
    } catch (ClassNotFoundException e) {
        logger.log(Level.WARNING, e.getMessage(), e);
    }
    return null;
}

From source file:com.bigdid.model.counters.java

public long getRevenueTotal() {

    List a = new ArrayList<String>();

    // Now the $group operation
    DBObject groupFields = new BasicDBObject("_id", null);
    groupFields.put("revenue", new BasicDBObject("$sum", "$Order_Value"));
    DBObject group = new BasicDBObject("$group", groupFields);

    coll = db.getCollection("item_revenue");

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(group);
    long total = 0l;
    AggregationOutput output = coll.aggregate(pipeline);

    for (DBObject result : output.results()) {
        //System.out.println(result.get("_id"));
        total = Long.valueOf(result.get("revenue").toString());
    }/*  ww  w .j  av  a  2s .  c  o  m*/
    return total;
}

From source file:com.bigdid.model.Feedback_Location.java

public List getCity() throws SQLException, UnknownHostException {

    List a = new ArrayList<String>();

    // Now the $group operation
    DBObject groupFields = new BasicDBObject("_id", "$Location");
    groupFields.put("revenue", new BasicDBObject("$sum", "$Order_Value"));
    DBObject group = new BasicDBObject("$group", groupFields);

    // Finally the $sort operation
    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("revenue", -1));

    DBObject limit = new BasicDBObject("$limit", 10);

    // run aggregation
    List<DBObject> pipeline = Arrays.asList(group, sort, limit);

    AggregationOutput output = coll.aggregate(pipeline);

    for (DBObject result : output.results()) {
        //System.out.println(result.get("_id"));
        a.add(result.get("_id"));
    }/*  w ww.  ja va 2  s .  c o m*/

    //numCity= l.size();
    return a;

}

From source file:com.bosscs.spark.mongodb.config.MongoDeepJobConfig.java

License:Apache License

/**
 * {@inheritDoc}//from  w ww .j a va2 s  .  c o m
 */
@Override
public MongoDeepJobConfig<T> ignoreIdField() {
    DBObject bsonFields = fields != null ? fields : new BasicDBObject();
    bsonFields.put("_id", 0);
    fields = bsonFields;
    return this;
}