mongodb 计算季度 & 按季度分组统计

阅读更多

 

 

计算季度:

 

db.task.aggregate([{
	$match: {
		"createDate": {$ne: null}
	}
}, {
	$project: {
		status: 1, 
		createDate: 1,
		quarter: {
			$cond: {
				if: {
					$gte: [{
						$month: "$createDate"
					}, 10]
				}, 
				then: "Q4", 
				else: {
					$cond: {
						if: {
							$gte: [{
								$month: "$createDate"
							}, 7]
						}, 
						then: "Q3", 
						else: {
							$cond: {
								if: {
									$gte: [{
										$month: "$createDate"
									}, 4]
								}, 
								then: "Q2", 
								else: "Q1"
							}
						}
					}
				}
			}
		}
	}
}])

 

 

按季度分组统计:

 

 

db.task.aggregate([{
	$match: {
		"createDate": {$ne: null}
	}
}, {
	$project: {
		status: 1, 
		createDate: 1,
		quarter: {
			$cond: {
				if: {
					$gte: [{
						$month: "$createDate"
					}, 10]
				}, 
				then: "Q4", 
				else: {
					$cond: {
						if: {
							$gte: [{
								$month: "$createDate"
							}, 7]
						}, 
						then: "Q3", 
						else: {
							$cond: {
								if: {
									$gte: [{
										$month: "$createDate"
									}, 4]
								}, 
								then: "Q2", 
								else: "Q1"
							}
						}
					}
				}
			}
		}
	}
}, {
	$group: {
		_id: {"year": {"$year": "$createDate"}, quarter: "$quarter", status: "$status"}, 
		count: { $sum: 1 }
	}
}, {
	$sort : {"_id.year" : -1, "_id.quarter": -1, "_id.status": -1}
}])

 

MongoDimensionAbstractService dm = new MongoDimensionAbstractService() {
    @Override
    public Aggregation newAggregation() {
        Date start = null;
        Date end = null;
        if (request != null) {
            start = request.getStart();
            end = request.getEnd();
        }

//                Criteria criteria = new Criteria();
        Criteria criteria = Criteria.where("createDate").ne(null);
        if (start != null || end != null) {
            if (start != null) {
                criteria.gte(start);
            }
            if (end != null) {
                criteria.lte(end);
            }
        }
        MatchOperation match = Aggregation.match(criteria);

        ProjectionOperation projection = Aggregation.project("_id", "status", "createDate");
        DBObject asObject = projection.toDBObject(Aggregation.DEFAULT_CONTEXT);

        ProjectionOperation.ProjectionOperationBuilder projectionOperationBuilder = projection.and("quarter");

        DateOperators.Month month = DateOperators.Month.monthOf("createDate");
        ComparisonOperators.Gte gte = ComparisonOperators.Gte.valueOf(month);
        gte = gte.greaterThanEqualToValue(10);
        ConditionalOperators.ConditionalOperatorFactory conditionalOperatorFactory = ConditionalOperators.when(gte);
        ConditionalOperators.Cond.OtherwiseBuilder otherwiseBuilder4 = conditionalOperatorFactory.then("Q4");

        gte = ComparisonOperators.Gte.valueOf(month);
        gte = gte.greaterThanEqualToValue(7);
        conditionalOperatorFactory = ConditionalOperators.when(gte);
        ConditionalOperators.Cond.OtherwiseBuilder otherwiseBuilder3 = conditionalOperatorFactory.then("Q3");

        gte = ComparisonOperators.Gte.valueOf(month);
        gte = gte.greaterThanEqualToValue(4);
        conditionalOperatorFactory = ConditionalOperators.when(gte);
        ConditionalOperators.Cond.OtherwiseBuilder otherwiseBuilder2 = conditionalOperatorFactory.then("Q2");


        projection = projectionOperationBuilder.applyCondition(otherwiseBuilder4.otherwiseValueOf(otherwiseBuilder3.otherwiseValueOf(otherwiseBuilder2.otherwise("Q1"))));


        DefaultAggregationOperation group = getDefaultAggregationOperationForReleaseStatusStatsAsQuarterly();

        SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "_id.year", "_id.quarter", "_id.status");
//                SortOperation sort = Aggregation.sort(Sort.Direction.DESC, "_id");
        Aggregation aggregation = Aggregation.newAggregation(match, projection, group, sort);//aggregation.withOptions(AggregationOptions);
        return aggregation;
    }
};

 

你可能感兴趣的:(mongodb 计算季度 & 按季度分组统计)