1. 程式人生 > >mongo aggregate 用法記錄

mongo aggregate 用法記錄

ride alt 政府 CA on() pan 技術 learning als

mongo 聚合查詢查詢還是很方便的,做下記錄 依賴的jar是org.springframework.data.mongodb 1.9.6 低版本可能不支持.

數據結構 大概是 這是一份問卷,問卷有15題,統計

技術分享圖片
{
    "_id": ObjectId("5a30eeef84aeea4fbcd6045c"),
    "_class": "com.fjhb.elearning.model.QuestionnaireSurvey",
    "questionnaireId": "321",
    "questionnaireType": "2",
    "completeTime": ISODate("2017-12-13T09:12:15.613+0000"),
    
"personMessage": { "phone": "13003831002", "jobType": NumberInt(2), "identity": "農村校校長、園長", "name": "", "studentId": "" }, "regionDto": { "_id": "350102", "parentId": "350100", "path": "/350000/350100/350102", "sort": NumberInt(0), "name": "鼓樓區",
"version": "GB/T2260-2007", "available": NumberInt(1), "beginTime": ISODate("2018-01-02T16:00:00.000+0000"), "endTime": ISODate("2018-01-03T16:00:00.000+0000") }, "basicQuestionnaireSurvey": [{ "_id": "1", "title": "1.您對本縣(市、區)政府在優先發展教育,積極推動教育事業發展的總體評價是:", "basicQuestionItems": [{
"_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "2", "title": "2.您對本縣(市、區)政府在促進教育公平、提高教育質量的總體評價是:", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "3", "title": "3.您對本縣(市、區)落實義務教育階段就學生近入學政策是否滿意?", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "4", "title": "4.您對本縣(市、區)解決義務教育階段擇校問題的評價是:", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "5", "title": "5.您對本縣(市、區)解決進城務工人員隨遷子女就學問題的評價是:", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "6", "title": "6. 您對本縣(市、區)采取措施縮小城區與農村學校之間、學校與學校之間的辦學條件差異是否滿意?", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "7", "title": "7.您對本縣(市、區)中小學校校舍場所和設施設備改善情況的評價是:", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "8", "title": "8.您對本縣(市、區)在尊師重教,改善中小學教師(包括幼兒園、中職校和特殊學校教師)待遇方面的評價是:", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "9", "title": "9.您對本縣(市、區)中小學校長的選用、教師的聘用和校際交流工作是否滿意? ", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "10", "title": "10. 您對本縣(市、區)在推進教學改革,發展素質教育,提高教育質量方面的評價是:", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "11", "title": "11. 您對本縣(市、區)重視中小學教師的業務培訓和專業發展,提高教師教書育人、立德樹人能力的評價是:", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "12", "title": "12. 您對本縣(市、區)做好中小學校、幼兒園的科學合理布局的評價是:", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "13", "title": "13.您對本縣(市、區)中小學校、幼兒園做好學校安全穩定工作是否滿意?", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "14", "title": "14.您對本縣(市、區)中小學校長(含幼兒園園長)的管理水平、教師的教學水平是否滿意?", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] }, { "_id": "15", "title": "15.您對本縣(市、區)中小學(含幼兒園)的教學質量是否滿意?", "basicQuestionItems": [{ "_id": "1", "select": true, "score": NumberInt(100), "content": "滿意" }, { "_id": "2", "select": false, "score": NumberInt(80), "content": "基本滿意" }, { "_id": "3", "select": false, "score": NumberInt(60), "content": "一般" }, { "_id": "4", "select": false, "score": NumberInt(40), "content": "不太滿意" }, { "_id": "5", "select": false, "score": NumberInt(20), "content": "不滿意" } ] } ], "platformId": "300417fe04e94eb9ac59807aa6db6bb2", "platformVersionId": "96f9bb883e3e40a09eea69a1b5e2aaf0", "projectId": "2c91128b5fa5b32c016010fb1fac111c", "subProjectId": "2c91128b5fa5b32c016010fb1fb5111e" }
數據結構 技術分享圖片
db.questionnaireSurvey.aggregate({"$unwind":"$basicQuestionnaireSurvey"},
{"$unwind":"$basicQuestionnaireSurvey.basicQuestionItems"},
{"$match":{"basicQuestionnaireSurvey.basicQuestionItems.select":{$eq:true}}},
{"$project":{qusetionId:"$basicQuestionnaireSurvey._id",
title:"$basicQuestionnaireSurvey.title",
satisfied:        {$cond: { if:  {$eq: ["$basicQuestionnaireSurvey.basicQuestionItems._id", "1" ] }, then: 1, else: 0 }},
basicSatisfaction:{$cond: { if: { $eq: ["$basicQuestionnaireSurvey.basicQuestionItems._id", "2" ] }, then: 1, else: 0 }},
commonly:{$cond: { if: { $eq: [ "$basicQuestionnaireSurvey.basicQuestionItems._id", "3" ] }, then: 1, else: 0 }},
notVerySatisfied:{$cond: { if: { $eq: [ "$basicQuestionnaireSurvey.basicQuestionItems._id", "4" ] }, then: 1, else: 0 }},
unSatisfied:{$cond: { if: { $eq: ["$basicQuestionnaireSurvey.basicQuestionItems._id", "5" ] }, then: 1, else: 0 }}
}},{"$group":{_id:{qusetionId:"$qusetionId",title:"$title"},satisfied:{$sum:"$satisfied"},
basicSatisfaction:{$sum:"$basicSatisfaction"},
commonly:{$sum:"$commonly"},
notVerySatisfied:{$sum:"$notVerySatisfied"},
unSatisfied:{$sum:"$unSatisfied"},
}}
);
sql 技術分享圖片
        List<AggregationOperation> list = new ArrayList<>();
        list.add(Aggregation.unwind("$basicQuestionnaireSurvey"));
        list.add(Aggregation.unwind("$basicQuestionnaireSurvey.basicQuestionItems"));
        list.add(Aggregation.match(Criteria.where("basicQuestionnaireSurvey.basicQuestionItems.select").is(true)));
        list.add(Aggregation.project("_id").and("$basicQuestionnaireSurvey._id").as("questionId").and("$basicQuestionnaireSurvey.title").as("title").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 100));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("satisfied").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 80));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("basicSatisfaction").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 60));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("commonly").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 40));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("notVerySatisfied").and(aggregationOperationContext -> {
                    DBObject condExpression = new BasicDBObject();
                    DBObject ifExpression = new BasicDBObject();
                    ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 20));
                    condExpression.put("if", ifExpression);
                    condExpression.put("then", 1);
                    condExpression.put("else", 0);
                    return new BasicDBObject("$cond", condExpression);
                }).as("unSatisfied").and(new AggregationExpression(){
                    @Override
                    public DBObject toDbObject(AggregationOperationContext context) {

                        DBObject condExpression = new BasicDBObject();
                        DBObject ifExpression = new BasicDBObject();
                        ifExpression.put("$eq", Arrays.asList("$basicQuestionnaireSurvey.basicQuestionItems.score", 20));
                        condExpression.put("if", ifExpression);
                        condExpression.put("then", 1);
                        condExpression.put("else", 0);
                        return new BasicDBObject("$cond", condExpression);

                    }
                })
        );
        list.add(Aggregation.group("questionId", "title").sum("satisfied").as("satisfied").sum("basicSatisfaction").as("basicSatisfaction").sum("commonly").as("commonly").sum("notVerySatisfied").as("notVerySatisfied").sum("unSatisfied").as("unSatisfied"));

        Aggregation agg = Aggregation.newAggregation(list);
        AggregationResults<MySyllabusAnswerCountKeyValue2> results = mongoTemplate.aggregate(agg, "questionnaireSurvey", MySyllabusAnswerCountKeyValue2.class);
        results.getMappedResults();
java 代碼

mongo aggregate 用法記錄