Aggregation

The method added in version 1.0.8 has not been simplified and the parameters are consistent with the official driver.

Simplified the use of aggregation operations with barely learning costs in Mongodb Plugin. Let's look at the following example:


MongoQuery query=new MongoQuery();
//Create a query object for storing conditions

query.use("student").eq("sex",1).descending("age").limit(10);
//Find ten students whose sex is “1” in reverse age order

MongoAggregation aggregation=new MongoAggregation(query);
//Create aggregate computing objects

logger.info(JSON.toJSONString(aggregation
.include("name","sex","age","teacherName") 
//Output only contains name, sex, age and teacherName

.projection() 
//If use include, exclude, excludeId, it must be added.

.lookup("teacher","teacherName","name","teacher")
//It is similar with Join query in mysql.

.out("_output_")
//Output results to the “_output_” collection

.pipeline(Aggregation operation)
//Put into bson generated by Aggregates method of official drive.

.aggregate()));
//Start computing

//Let's look at another example of computing all boys' average scores.

MongoQuery query=new MongoQuery();

query.use("student").eq("sex",1).descending("teacherName");
//According to the teacher reverse name, it first search the ranking and then computing. Good ranking and conditions can accelerate the speed of operation.

MongoAggregation aggregation=new MongoAggregation(query);

logger.info(JSON.toJSONString(aggregation
.group("$teacherName",new MongoAccumulator().avg("result","$score"))
.aggregate()));
//Placing the field name $ in “group” represents distinguishing different outcomes by it in this table. (such as the average score for all students belongs to teacher A and teacher B).MongoAccumulator is a packaged formula in MongoPlugin, which basically contains operation formula of mongodb Java driver., It aims at computing the average score in this example. Take score field as calculation basis and put the result into result field.

results matching ""

    No results matching ""