Example usage for com.mongodb DBCollection aggregate

List of usage examples for com.mongodb DBCollection aggregate

Introduction

In this page you can find the example usage for com.mongodb DBCollection aggregate.

Prototype

public Cursor aggregate(final List<? extends DBObject> pipeline, final AggregationOptions options) 

Source Link

Document

Method implements aggregation framework.

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

    }

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

    return stringResult;

}

From source file:com.ebay.cloud.cms.dal.persistence.MongoExecutor.java

License:Apache License

public static AggregationOutput aggregate(PersistenceContext context, MetaClass metadata, DBObject firstObject,
        DBObject... aggrObjects) {//from www .j a v a2s.c  o m
    long start = System.currentTimeMillis();
    String msg = "success";
    DBCollection dbCollection = context.getDBCollection(metadata);
    AggregationOutput output = null;
    try {
        output = dbCollection.aggregate(firstObject, aggrObjects);
        if (!output.getCommandResult().ok()) {
            throw new CmsDalException(DalErrCodeEnum.AGGREGATION_FAILED,
                    output.getCommandResult().getErrorMessage());
        }
    } catch (Throwable t) {
        msg = t.getMessage();
        handleMongoException(t);
    } finally {
        DBObject followingOjbect = new BasicDBObject();
        followingOjbect.put("following aggreate operations: ", aggrObjects);
        logMongoAction(context, "aggregate", start, dbCollection, firstObject, followingOjbect, null, null,
                msg);
    }
    return output;
}

From source file:com.github.maasdi.mongo.wrapper.NoAuthMongoClientWrapper.java

License:Apache License

private static Iterator<DBObject> setUpPipelineSample(String query, int numDocsToSample,
        DBCollection collection) throws KettleException {

    query = query + ", {$limit : " + numDocsToSample + "}"; //$NON-NLS-1$ //$NON-NLS-2$
    List<DBObject> samplePipe = jsonPipelineToDBObjectList(query);

    DBObject first = samplePipe.get(0);/*from  w  ww  . j  a  va2  s.c o  m*/
    DBObject[] remainder = new DBObject[samplePipe.size() - 1];
    for (int i = 1; i < samplePipe.size(); i++) {
        remainder[i - 1] = samplePipe.get(i);
    }

    AggregationOutput result = collection.aggregate(first, remainder);

    return result.results().iterator();
}

From source file:com.mingo.executor.AbstractQueryExecutor.java

License:Apache License

/**
 * Perform aggregation query./* w  ww.  j  a v a 2  s . co  m*/
 *
 * @param dbCollection db collection
 * @param query        query
 * @return {@link AggregationOutput}
 */
protected AggregationOutput performAggregationQuery(DBCollection dbCollection, String query) {
    Assert.notNull(dbCollection, "dbCollection cannot be null");
    Assert.hasText(query, "query cannot be null or empty");
    BasicDBList operatorsDB = pipelineBuilder.buildAggregation(query);
    DBObject firstOperator = (DBObject) operatorsDB.remove(FIRST_ELEMENT);
    return dbCollection.aggregate(firstOperator, operatorsDB.toArray(new DBObject[FIRST_ELEMENT]));
}

From source file:com.mingo.repository.impl.ReviewRepository.java

License:Apache License

private AggregationOutput aggregate(String dbCollection, final List<DBObject> operators) {
    Assert.hasText(dbCollection, "collection name cannot be null");
    Assert.notEmpty(operators,/*from  w  w w  .  jav  a 2  s  . c om*/
            "Operators cannot be null or empty. " + "Collection must contains at least one operator.");
    AggregationOutput aggregationOutput = getMongoTemplate().execute(dbCollection,
            new CollectionCallback<AggregationOutput>() {
                @Override
                public AggregationOutput doInCollection(DBCollection collection)
                        throws MongoException, DataAccessException {
                    DBObject firstOperator = operators.remove(FIRST_ELEMENT);
                    return collection.aggregate(firstOperator, operators.toArray(new DBObject[FIRST_ELEMENT]));
                }
            });
    return aggregationOutput;
}

From source file:com.openbravo.data.loader.MongoDBPreparedSentence.java

@Override
public DataResultSet openExec(Object params) throws BasicException {
    closeExec();//from   w w w  .  j av  a 2s.  co  m

    DBCollection collection = m_s.getMongoDBDatabase().getCollection(m_collectionName);

    if (m_SerWrite != null) {
        if (m_insert)
            m_SerWrite.writeValues(new MongoDBPreparedSentencePars(m_insertDBObject, m_writeParamColumnMap),
                    params);
        else if (m_find)
            m_SerWrite.writeValues(new MongoDBPreparedSentencePars(m_findDBObject, m_writeParamColumnMap),
                    params);
        else if (m_update)
            m_SerWrite.writeValues(new MongoDBPreparedSentencePars(m_updateDBObject, m_writeParamColumnMap),
                    params);
    }

    if (!m_nullColumn.isEmpty())
        m_findDBObject.append(m_nullColumn, new BasicDBObject("$exists", true));

    if (m_lessThanColumn != null) {
        for (int i = 0; i < m_lessThanColumn.length; ++i) {
            Object lessThanValue = m_findDBObject.get(m_lessThanColumn[i]);
            m_findDBObject.removeField(m_lessThanColumn[i]);
            m_findDBObject.append(m_lessThanColumn[i], new BasicDBObject("$lt", lessThanValue));
        }
    }

    if (m_greaterThanColumn != null) {
        for (int i = 0; i < m_greaterThanColumn.length; ++i) {
            Object greaterThanValue = m_findDBObject.get(m_greaterThanColumn[i]);
            m_findDBObject.removeField(m_greaterThanColumn[i]);
            m_findDBObject.append(m_greaterThanColumn[i], new BasicDBObject("$gt", greaterThanValue));
        }
    }

    // Insert statement
    if (m_insert) {
        collection.insert(m_insertDBObject);
    } else if (m_find) {

        if (!m_sortColumn.isEmpty()) {
            m_dbCursor = collection.find(m_findDBObject).sort(new BasicDBObject(m_sortColumn, 1));
            return new MongoDBDataResultSet(m_dbCursor, m_readParamColumnMap, m_SerRead);
        }

        if (!m_maxColumn.isEmpty()) {
            Iterator<DBObject> it = collection
                    .aggregate(new BasicDBObject("$match", m_findDBObject),
                            new BasicDBObject("$group",
                                    new BasicDBObject("_id", "null").append("MAX",
                                            new BasicDBObject("$max", "$" + m_maxColumn))))
                    .results().iterator();
            //Iterator<DBObject> it = collection.aggregate(new BasicDBObject("$group", new BasicDBObject("_id", "null").append("MAX", new BasicDBObject("$max", "$" + m_maxColumn)))).results().iterator();
            DBObject maxObject = new BasicDBObject();
            if (it.hasNext())
                maxObject = it.next();
            return new MongoDBDataResultSet(maxObject, m_readParamColumnMap, m_SerRead);
        }

        if (m_countAll) {
            Iterator<DBObject> it = collection
                    .aggregate(new BasicDBObject("$match", m_findDBObject), new BasicDBObject("$group",
                            new BasicDBObject("_id", "null").append("COUNT", new BasicDBObject("$sum", 1))))
                    .results().iterator();
            //Iterator<DBObject> it = collection.aggregate(new BasicDBObject("$group", new BasicDBObject("_id", "null").append("MAX", new BasicDBObject("$max", "$" + m_maxColumn)))).results().iterator();
            DBObject maxObject = new BasicDBObject();
            if (it.hasNext())
                maxObject = it.next();
            return new MongoDBDataResultSet(maxObject, m_readParamColumnMap, m_SerRead);
        }
        m_dbCursor = collection.find(m_findDBObject);
        return new MongoDBDataResultSet(m_dbCursor, m_readParamColumnMap, m_SerRead);
    } else if (m_update) {
        String findKey = ((String) m_writeParamColumnMap
                .get((Integer) m_writeParamColumnMap.keySet().toArray()[m_writeParamColumnMap.size() - 1]));
        String key = findKey.replace('s', ' ').trim();
        m_findDBObject.append(key, m_updateDBObject.get(findKey));

        // Remove the find criteria in the update object
        m_updateDBObject.remove(m_writeParamColumnMap
                .get((Integer) m_writeParamColumnMap.keySet().toArray()[m_writeParamColumnMap.size() - 1]));
        collection.findAndModify(m_findDBObject, null, null, true, m_updateDBObject, true, true);
        return new SentenceUpdateResultSet(0);
    }
    return null;
}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public List<BasicDBObject> aggregate(String property, Filter filter, Boolean getStats) {
    LOG.debug("Starting aggregation for the following property: {}", property);
    long start = System.currentTimeMillis();
    Property prop = getCache().getProperty(property);
    String propType = prop.getType();

    List<BasicDBObject> result = new ArrayList<BasicDBObject>();
    DBCollection collection = this.getCollection(Element.class);
    BasicDBList basicAggregationStages = new BasicDBList();
    BasicDBList basicDBList = new BasicDBList();

    if (propType.equals(PropertyType.STRING.toString())) {
        basicAggregationStages = getBasicAggregationStages(property, filter, "$sourcedValue.value");
    } else if (propType.equals(PropertyType.INTEGER.toString())
            || propType.equals(PropertyType.FLOAT.toString())) { //TODO: choose a better strategy to address this. Think of bins for numerical values.
        basicAggregationStages = getBasicAggregationStages(property, filter, "$sourcedValue.value");
    } else if (propType.equals(PropertyType.DATE.toString())) {

        /* BasicDBList cond = new BasicDBList();
         BasicDBList eq = new BasicDBList();
         eq.add("$sourcedValue.value");//from  ww w . j a v  a  2 s. c o m
         eq.add(0);
         cond.add(new BasicDBObject("$ifNull", eq));
         cond.add(new BasicDBObject("$year", "$sourcedValue.value"));
         cond.add(-1);
        */

        BasicDBObject conditionalValue = new BasicDBObject("$year", "$sourcedValue.value");
        basicAggregationStages = getBasicAggregationStages(property, filter, conditionalValue);
    } else if (propType.equals(PropertyType.BOOL.toString())) {
        basicAggregationStages = getBasicAggregationStages(property, filter, "$sourcedValue.value");
    }
    if (getStats)
        basicAggregationStages.add(new BasicDBObject("$group",
                new BasicDBObject("_id", "$property")
                        .append("stdDev", new BasicDBObject("$stdDevPop", "$value"))
                        .append("min", new BasicDBObject("$min", "$value"))
                        .append("max", new BasicDBObject("$max", "$value"))
                        .append("avg", new BasicDBObject("$avg", "$value"))
                        .append("sum", new BasicDBObject("$sum", "$value"))
                        .append("count", new BasicDBObject("$sum", 1))));
    else {
        if (propType.equals(PropertyType.INTEGER.toString()))
            basicAggregationStages.add(new BasicDBObject("$bucketAuto",
                    new BasicDBObject("groupBy", "$value").append("buckets", 10)));
        // basicAggregationStages.add(new BasicDBObject("$group", new BasicDBObject("_id", "$value").append("count", new BasicDBObject("$sum", 1))));
    }
    //AggregationOutput aggregate = collection.aggregate(basicAggregationStages);

    String s = basicAggregationStages.toString();
    List<DBObject> pipeline = new ArrayList<DBObject>();
    for (Object basicAggregationStage : basicAggregationStages) {
        pipeline.add((DBObject) basicAggregationStage);
    }
    AggregationOptions build = AggregationOptions.builder().allowDiskUse(true).build();
    Cursor aggregate = collection.aggregate(pipeline, build);
    // while(aggregate.hasNext()){
    //     result.add((BasicDBObject) aggregate.next());
    //  }

    //Iterable<DBObject> resultIterable = collection.aggregate(pipeline,build).results();
    //for (DBObject object : resultIterable) {
    //    result.add((BasicDBObject) object);
    //}

    long end = System.currentTimeMillis();
    LOG.debug("The aggregation job took {} seconds", (end - start) / 1000);

    return result;
}

From source file:datapreparation.MongoStatistics.java

public void usersOfUrl(String URL) {
    // TODO code application logic here

    // TODO code application logic here
    int limit = 0;
    String filename = "Users_Of_Url_" + URL + ".txt";

    // To directly connect to a single MongoDB server (note that this will not auto-discover the primary even
    MongoClient mongoClient;/*  w  w w .  j a va2  s  . com*/

    try {
        mongoClient = new MongoClient("localhost");

        //use database
        DB db = mongoClient.getDB("users");

        //get collection
        DBCollection coll = db.getCollection("urls");

        // build the $projection operation
        //            DBObject fields = new BasicDBObject("user", 1);
        //            fields.put("_id", 0);
        //            BasicDBObject project = new BasicDBObject("$project", fields);

        //build the match operation
        DBObject matchFields = new BasicDBObject("url", URL);
        DBObject match = new BasicDBObject("$match", matchFields);

        // Now the $group operation
        DBObject groupFields = new BasicDBObject("_id", "$user");
        groupFields.put("count", new BasicDBObject("$sum", 1));
        DBObject group = new BasicDBObject("$group", groupFields);

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

        // run aggregation
        List<DBObject> pipeline;
        if (limit == 0) {// without limits!
            pipeline = Arrays.asList(match, group, sort);
        } else {
            // create new BasicDBObject that limit query result in only 100 rows
            DBObject limitRes = new BasicDBObject("$limit", limit);
            pipeline = Arrays.asList(match, group, sort, limitRes);
        }
        AggregationOptions aggregationOptions = AggregationOptions.builder().batchSize(100)
                .outputMode(AggregationOptions.OutputMode.CURSOR).allowDiskUse(true).build();

        Cursor cursor = coll.aggregate(pipeline, aggregationOptions);

        writeToFile(cursor, filename, "User\t Count");

        cursor.close();
        mongoClient.close();

    } catch (IOException ex) {
        System.out.println("Something's Wrong! " + ex);
    }
}