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,
        final ReadPreference readPreference) 

Source Link

Document

Method implements aggregation framework.

Usage

From source file:com.seer.datacruncher.jpa.dao.MongoDbDao.java

License:Open Source License

/**
 * Gets data for Annual, Detailed, Monthly reports.
 *
 *        db.jv_datastreams.aggregate({$match : {id_application : 2}},
 *    {$project : {_id : 0, checked : 1, received_date : 1}},
 *    {$group: { _id: "$checked", result_sum : {$sum : 1}, result_date :
 *    {$first : "$received_date"}}})/* w w  w  . j a v  a 2 s.com*/
 *    + date_range
 *
 * @return reports data
 */
public static List<?> getList(ReportType type, int appId, int schemaId, Calendar currentDate) {
    List<Object> list = new ArrayList<Object>();
    DBCollection coll = getDatastreamsCollection();
    if (type == ReportType.MONTHLY || type == ReportType.ANNUAL || type == ReportType.DETAILED) {
        // $match operation
        DBObject matchFields = new BasicDBObject(_APP_ID, appId);
        if (type == ReportType.DETAILED) {
            matchFields.put(_SCHEMA_ID, schemaId);
        }
        DBObject dateRange = new BasicDBObject("$gt", ReportsUtils.getStartDate(type, currentDate));
        dateRange.put("$lt", ReportsUtils.getEndDate(type, currentDate));
        matchFields.put(_RECEIVED_DATE, dateRange);
        DBObject match = new BasicDBObject("$match", matchFields);
        // $projection operation
        DBObject fields = new BasicDBObject("_id", 0);
        fields.put(_CHECKED, 1);
        fields.put(_RECEIVED_DATE, 1);
        DBObject project = new BasicDBObject("$project", fields);
        // $group operation
        DBObject groupFields = new BasicDBObject("_id", "$" + _CHECKED);
        final String resultSum = "result_sum";
        final String resultDate = "result_date";
        groupFields.put(resultSum, new BasicDBObject("$sum", 1));
        groupFields.put(resultDate, new BasicDBObject("$first", "$" + _RECEIVED_DATE));
        DBObject group = new BasicDBObject("$group", groupFields);
        AggregationOutput aggr = coll.aggregate(match, project, group);
        Iterator<DBObject> it = aggr.results().iterator();
        while (it.hasNext()) {
            DBObject obj = it.next();
            Object[] arr = new Object[3];
            arr[0] = obj.get("_id");
            arr[1] = obj.get(resultSum);
            arr[2] = new SimpleDateFormat("yyyy-MM-dd").format(obj.get(resultDate));
            list.add(arr);
        }
    }
    return list;
}

From source file:com.seer.datacruncher.jpa.dao.MongoDbDao.java

License:Open Source License

/**
 * Gets data for Donut and RealTime report.
 *
 *        db.jv_datastreams.aggregate({$match : {id_application : 2}},
 *    {$project : {_id : 0, checked : 1, received_date : 1}},
 *    {$group: { _id: "$checked", result_sum : {$sum : 1}, result_date :
 *    {$first : "$received_date"}}})/*w  ww .  j  a v a2  s .  c  o  m*/
 *    + date_range
 *
 * @return
 */
public static List<?> getList(int appId, int schemaId, int year, int month, Calendar currentDate,
        boolean isDonut) {
    List<Object> list = new ArrayList<Object>();
    DBCollection coll = getDatastreamsCollection();
    DBObject matchFields = new BasicDBObject();
    if (isDonut) {
        matchFields.put(_APP_ID, appId);
        if (schemaId != 0) {
            matchFields.put(_SCHEMA_ID, schemaId);
        }
    }
    DBObject dateRange = new BasicDBObject("$gt", isDonut ? ReportsUtils.getDonutStartDate(year, month)
            : ReportsUtils.getStartDate(ReportType.REAL_TIME, currentDate));
    dateRange.put("$lt", isDonut ? ReportsUtils.getDonutEndDate(year, month)
            : ReportsUtils.getEndDate(ReportType.REAL_TIME, currentDate));
    matchFields.put(_RECEIVED_DATE, dateRange);
    DBObject match = new BasicDBObject("$match", matchFields);
    // $projection operation
    DBObject fields = new BasicDBObject("_id", 0);
    fields.put(_CHECKED, 1);
    DBObject project = new BasicDBObject("$project", fields);
    // $group operation
    DBObject groupFields = new BasicDBObject("_id", "$" + _CHECKED);
    final String resultSum = "result_sum";
    groupFields.put(resultSum, new BasicDBObject("$sum", 1));
    DBObject group = new BasicDBObject("$group", groupFields);
    AggregationOutput aggr = coll.aggregate(match, project, group);
    Iterator<DBObject> it = aggr.results().iterator();
    while (it.hasNext()) {
        DBObject obj = it.next();
        Object[] arr = new Object[2];
        arr[0] = obj.get("_id");
        arr[1] = Long.valueOf((Integer) obj.get(resultSum));
        list.add(arr);
    }
    return list;
}

From source file:com.tengen.home.Week1Homework3.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();

    DB database = client.getDB("m101");
    DBCollection collection = database.getCollection("funnynumbers");

    // Not necessary yet to understand this.  It's just to prove that you
    // are able to run a command on a mongod server
    AggregationOutput output = collection.aggregate(
            new BasicDBObject("$group",
                    new BasicDBObject("_id", "$value").append("count", new BasicDBObject("$sum", 1))),
            new BasicDBObject("$match", new BasicDBObject("count", new BasicDBObject("$gt", 2))),
            new BasicDBObject("$sort", new BasicDBObject("_id", 1)));

    int answer = 0;
    for (DBObject doc : output.results()) {
        answer += (Double) doc.get("_id");
    }/*from w  w  w. jav  a  2 s  .  c om*/

    System.out.println("THE ANSWER IS: " + answer);
}

From source file:com.tengen.home.Week1Homework4.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(Week1Homework4.class, "/");

    MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));

    DB database = client.getDB("m101");
    final DBCollection collection = database.getCollection("funnynumbers");

    Spark.get(new Route("/") {
        @Override// w  w w.j a v  a2 s.com
        public Object handle(final Request request, final Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template helloTemplate = configuration.getTemplate("answer.ftl");

                // Not necessary yet to understand this.  It's just to prove that you
                // are able to run a command on a mongod server
                AggregationOutput output = collection.aggregate(
                        new BasicDBObject("$group",
                                new BasicDBObject("_id", "$value").append("count",
                                        new BasicDBObject("$sum", 1))),
                        new BasicDBObject("$match", new BasicDBObject("count", new BasicDBObject("$lte", 2))),
                        new BasicDBObject("$sort", new BasicDBObject("_id", 1)));

                int answer = 0;
                for (DBObject doc : output.results()) {
                    answer += (Double) doc.get("_id");
                }

                Map<String, String> answerMap = new HashMap<String, String>();
                answerMap.put("answer", Integer.toString(answer));

                helloTemplate.process(answerMap, writer);
            } catch (Exception e) {
                logger.error("Failed", e);
                halt(500);
            }
            return writer;
        }
    });
}

From source file:DashboardServices.FetchInstances.java

@GET
@Produces(MediaType.TEXT_PLAIN)/* w  w  w . j a  v a 2 s . c  o m*/
public Response getRole(@Context UriInfo info) {
    String projectId = info.getQueryParameters().getFirst("projectId");
    String entityName = info.getQueryParameters().getFirst("entityName");
    System.out.println("projectId" + projectId);
    String finalOutput = "";
    try {
        MongoClient mongoClient;
        try {
            mongoClient = new MongoClient();
            // Now connect to your databases
            DB db = mongoClient.getDB("SSKDatabase");
            //System.out.println("Connect to database successfully");
            DBCollection coll = db.getCollection("insa");

            DBObject match = new BasicDBObject("$match", new BasicDBObject("_id", projectId));
            DBObject unwind = new BasicDBObject("$unwind", "$" + entityName);
            DBObject project = new BasicDBObject("$project", new BasicDBObject("instances", "$" + entityName));

            AggregationOutput output = coll.aggregate(match, unwind, project);
            if (output != null) {
                System.out.println("Ajay worked");
            } else {
                System.out.println("ajay didnt not woek");
            }
            System.out.println("before if condition");
            for (DBObject result : output.results()) {
                System.out.println("hai i reached here");
                System.out.println(result);
                finalOutput += result;
            }
        } catch (Exception e) {
            System.out.println("error");
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return Response.status(200).entity(finalOutput).build();
}

From source file:mongodb.JavaAggregate.java

public static void largeSmallVowels(DBCollection collection) {
    BasicDBObject match = new BasicDBObject("$match",
            new BasicDBObject("first", new BasicDBObject("$in", new String[] { "a", "e", "i", "o", "u" })));
    BasicDBObject groupOps = new BasicDBObject("_id", "$first");
    groupOps.append("largest", new BasicDBObject("$max", "$size"));
    groupOps.append("smallest", new BasicDBObject("$min", "$size"));
    groupOps.append("total", new BasicDBObject("$sum", 1));
    BasicDBObject group = new BasicDBObject("$group", groupOps);
    BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("first", 1));
    AggregationOutput result = collection.aggregate(match, group, sort);
    System.out.println("\nLargest and smallest word sizes for " + "words beginning with a vowel: ");
    JavaAggregate.displayAggregate(result);
}

From source file:mongodb.JavaAggregate.java

public static void top5AverageWordFirst(DBCollection collection) {
    BasicDBObject groupOps = new BasicDBObject("_id", "$first");
    groupOps.append("average", new BasicDBObject("$avg", "$size"));
    BasicDBObject group = new BasicDBObject("$group", groupOps);
    BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("average", -1));
    BasicDBObject limit = new BasicDBObject("$limit", 5);
    AggregationOutput result = collection.aggregate(group, sort, limit);
    System.out.println("\nFirst letter of top 5 largest average " + "word size: ");
    JavaAggregate.displayAggregate(result);
}

From source file:week1.Week1Homework3.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    MongoClient client = new MongoClient();

    DB database = client.getDB("m101");
    DBCollection collection = database.getCollection("funnynumbers");

    // Not necessary yet to understand this. It's just to prove that you
    // are able to run a command on a mongod server
    AggregationOutput output = collection.aggregate(
            new BasicDBObject("$group",
                    new BasicDBObject("_id", "$value").append("count", new BasicDBObject("$sum", 1))),
            new BasicDBObject("$match", new BasicDBObject("count", new BasicDBObject("$gt", 2))),
            new BasicDBObject("$sort", new BasicDBObject("_id", 1)));

    int answer = 0;
    for (DBObject doc : output.results()) {
        answer += (Double) doc.get("_id");
    }/*from w w w  .  j a  v  a 2 s. c om*/

    System.out.println("THE ANSWER IS: " + answer);
}

From source file:week1.Week1Homework4.java

License:Apache License

public static void main(String[] args) throws UnknownHostException {
    final Configuration configuration = new Configuration();
    configuration.setClassForTemplateLoading(Week1Homework4.class, "/");

    MongoClient client = new MongoClient(new ServerAddress("localhost", 27017));

    DB database = client.getDB("m101");
    final DBCollection collection = database.getCollection("funnynumbers");

    Spark.get(new Route("/") {
        @Override/*w w  w . j a va2  s .c o m*/
        public Object handle(final Request request, final Response response) {
            StringWriter writer = new StringWriter();
            try {
                Template helloTemplate = configuration.getTemplate("answer.ftl");

                // Not necessary yet to understand this. It's just to prove
                // that you
                // are able to run a command on a mongod server
                AggregationOutput output = collection.aggregate(
                        new BasicDBObject("$group",
                                new BasicDBObject("_id", "$value").append("count",
                                        new BasicDBObject("$sum", 1))),
                        new BasicDBObject("$match", new BasicDBObject("count", new BasicDBObject("$lte", 2))),
                        new BasicDBObject("$sort", new BasicDBObject("_id", 1)));

                int answer = 0;
                for (DBObject doc : output.results()) {
                    answer += (Double) doc.get("_id");
                }

                Map<String, String> answerMap = new HashMap<String, String>();
                answerMap.put("answer", Integer.toString(answer));

                helloTemplate.process(answerMap, writer);
            } catch (Exception e) {
                logger.error("Failed", e);
                halt(500);
            }
            return writer;
        }
    });
}

From source file:yelpapp.HW3.java

public SearchResults getUserDetailsQuery(Date y_membersince, String review_condition, String review_value,
        String noofFriends_cond, String nooffriendsvalue, String avgstarscond, String avgstarsVal,
        String maincondition, String userVotesCond, String uservotesValue) {
    SearchResults SearchResults = new SearchResults();

    /*/*  w  w w .j  a  v  a  2 s . c o  m*/
            
    select y_name,Y_YELPING_SINCE, Y_AVERAGE_STARS
    from y_user_tb yusr 
    where yusr.Y_REVIEW_COUNT = ?
    and yusr.Y_FRIENDS_COUNT = ?
    and yusr.Y_AVERAGE_STARS =?
    */StringBuilder userResultStr = new StringBuilder();
    Connection dbconnection;
    // dbconnection = YelpDBConnectionFactory.connectDatabase();
    DB db1 = new MongoClient("localhost", 27017).getDB("YelpApplication");
    DBCollection dbcollection = db1.getCollection("YelpUser");
    String datestr = "2014-12-09T00:00:00Z";
    if (isdebug)
        System.out.println("start" + " ISODate(" + datestr + ")");

    String query = "";
    List<String> userResults = new ArrayList<String>();
    int count = 0;
    try {

        boolean firstwhereclause = true;
        String mainconditiona = " and ";

        if (isnotnullornotempty(maincondition)) {
            maincondition = maincondition.trim();
            if (isdebug)
                System.out.println("maincondition=" + maincondition);
            if ("and, or".equalsIgnoreCase(maincondition)) {
                maincondition = " and ";
                if (isdebug)
                    System.out.println("maincondition=" + maincondition);
            }
            ;
        }

        else
            maincondition = mainconditiona;

        String option = maincondition.equalsIgnoreCase("and") ? "$and" : "$or";
        DateFormat df = new SimpleDateFormat("yyyy-mm-dd");
        String date_string = df.format(y_membersince);
        if (isdebug)
            System.out.println("date_string + " + date_string + "y_membersince  " + y_membersince);
        BasicDBObject reviewcountcondDBOB = null;
        BasicDBObject reviewcountcond1 = null;
        BasicDBObject yelpingsincecondDBOB = null;
        BasicDBObject yelpingsince = null;
        BasicDBObject friendscondDOB = null;
        BasicDBObject friendsDOB = null;
        BasicDBObject averagestarscondDBOB = null;
        BasicDBObject averagestarscond1 = null;
        BasicDBObject uservotesDOB = null;
        userResultStr = new StringBuilder();
        if (isnotnullornotempty(date_string) && isnotnullornotempty(date_string)) {
            String yelpingcond = "$gt";
            yelpingsincecondDBOB = new BasicDBObject(yelpingcond, y_membersince);
            yelpingsince = new BasicDBObject("yelping_since", yelpingsincecondDBOB);
            if (isdebug)
                System.out.println("yelpingsince  =" + yelpingsince);
        }

        if (isnotnullornotempty(review_value) && isnotnullgezero(review_value)
                && isnotnullornotempty(review_condition)) {

            String reviewcond = condstr(review_condition);
            reviewcountcondDBOB = new BasicDBObject(reviewcond, Integer.parseInt(review_value));
            reviewcountcond1 = new BasicDBObject("review_count", reviewcountcondDBOB);
            System.out.println("Reviw condition =" + reviewcountcond1);

        }

        if (isnotnullornotempty(nooffriendsvalue) && isnotnullgezero(nooffriendsvalue)
                && isnotnullornotempty(noofFriends_cond)) {
            String yelpingcond = condstr(noofFriends_cond);
            friendscondDOB = new BasicDBObject(yelpingcond, Double.parseDouble(nooffriendsvalue));
            friendsDOB = new BasicDBObject("friendsCount", friendscondDOB);
            if (isdebug)
                System.out.println("friendsDOB  =" + friendsDOB);
        }
        if (isnotnullornotempty(userVotesCond) && isnotnullgezero(uservotesValue)
                && isnotnullornotempty(userVotesCond)) {
            String yelpingcond = condstr(userVotesCond);
            friendscondDOB = new BasicDBObject(yelpingcond, Integer.parseInt(uservotesValue));
            uservotesDOB = new BasicDBObject("totalVotes", friendscondDOB);
            //System.out.println("uservotesDOB  ="+uservotesDOB);
        }
        if (isnotnullornotempty(avgstarsVal) && isnotnullgezero(avgstarsVal)
                && isnotnullornotempty(avgstarscond)) {
            String starscond = condstr(avgstarscond);
            averagestarscondDBOB = new BasicDBObject(starscond, Integer.parseInt(avgstarsVal));
            averagestarscond1 = new BasicDBObject("average_stars", averagestarscondDBOB);
            System.out.println("averagestarscond1  =" + averagestarscond1);
        }

        //query to database 

        /* PreparedStatement statement;
            statement = dbconnection.prepareStatement(query);
                    
           ResultSet rs =  statement.executeQuery();
           count=0;
           while(rs.next()) {
               UserDetails useDetails = new  UserDetails();
               useDetails.setYname(rs.getString("y_name"));
               java.util.Date sqlDate;
            sqlDate = new java.util.Date(rs.getDate("Y_YELPING_SINCE").getTime());
             df = new SimpleDateFormat("yyyy-MM");
             df.format(sqlDate);
               useDetails.setYelp_Since(df.format(sqlDate));
                       
               useDetails.setAverage_stars(rs.getString("Y_AVERAGE_STARS"));
                       
               useDetails.setFriends_count(rs.getString("Y_FRIENDS_COUNT"));
           useDetails.setYid(rs.getString("y_id"));
               userResults.add(useDetails);
               count++;
           }*/
        List<BasicDBObject> asList = new ArrayList<BasicDBObject>();
        asList.add(yelpingsince);//reviewcountcond1,averagestarscond1,friendsDOB
        if (null != reviewcountcond1)
            asList.add(reviewcountcond1);
        if (null != averagestarscond1)
            asList.add(averagestarscond1);
        if (null != friendsDOB)
            asList.add(friendsDOB);
        if (null != uservotesDOB)
            asList.add(uservotesDOB);
        DBObject optioncond = new BasicDBObject(option, asList);
        System.out.println("optioncond  =" + optioncond);
        System.out.println("option" + option);
        DBObject match = new BasicDBObject("$match", optioncond);
        System.out.println("match  =" + match);
        DBObject project = new BasicDBObject("$project", new BasicDBObject("name", 1).append("average_stars", 1)
                .append("review_count", 1).append("yelping_since", 1).append("_id", 0));
        DBObject limoit = new BasicDBObject("$limit", 200);
        System.out.println(match);
        AggregationOutput output = dbcollection.aggregate(match, project, limoit);
        System.out.println("output" + output);
        query = match.toString();
        userResults = new ArrayList<String>();
        for (DBObject result : output.results()) {
            System.out.println(result);
            userResults.add(result.toString());
            userResultStr.append(result.toString());
        }

    } catch (Exception e) {
        e.printStackTrace();

    } finally {

    }
    System.out.println(query);
    /*  SearchResults.setIsusersearchResults(true);
      SearchResults.numberofrecords=count;
      SearchResults.setUserSearchresults(userResults);
      SearchResults.Query = query;*/
    System.out.println(query);
    SearchResults.setIsusersearchResults(true);
    SearchResults.numberofrecords = count;
    SearchResults.jason = userResultStr.toString();
    SearchResults.setUserSearchresults(userResults);
    SearchResults.setUserStr(userResults);
    SearchResults.Query = query;
    return SearchResults;
}