如何使用聚合框架在MongoDB java中提供多个预测

时间:2016-02-08 13:43:54

标签: mongodb mongodb-query aggregation-framework mongodb-java

我有三个文件

{ "_id" : 3, "quizzes" : [ 4, 5, 5 ], "labs" : [ 6, 5 ], "final" : 78, "midterm" : 70 }
{ "_id" : 2, "quizzes" : [ 9, 10 ], "labs" : [ 8, 8 ], "final" : 95, "midterm" : 80 }
{ "_id" : 1, "quizzes" : [ 10, 6, 7 ], "labs" : [ 5, 8 ], "final" : 80, "midterm" : 75, "extraMarks" : 10 }

我必须将考试总字段作为最终+期中+额外的总和来查询。 查询如下: 的查询1:

  db.students.aggregate([ { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] } } } ])

但是,除此之外,我需要仅针对id大于等于1且小于2的人

所以我已将代码修改为以下内容: 的 QUERY2:

 db.students.aggregate([ { $match: { $and: [ { _id: { $gte: 1, $lte: 2 } }]}}, { "$project": { "final": 1, "midterm": 1, "examTotal": { "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] } } } ])

如何将此代码转换为查询1的Java代码我创建了代码并且正在运行?如何在现有代码中添加匹配阶段,如下所示:

BsonArray fields=new BsonArray();
            BsonArray defaultValue1=new BsonArray();
            defaultValue1.add(new BsonString("$extraMarks"));
            defaultValue1.add(new BsonDouble(0d));          
            BsonDocument ifNullProjection=new BsonDocument();
            ifNullProjection.put("$ifNull",defaultValue1);
            fields.add(new BsonString("$final"));
            fields.add("$midterm");
            fields.add(ifNullProjection);
            BsonDocument addObject=new BsonDocument();
            addObject.append("$add", fields);
            BsonDocument valueTobeUpdate=new BsonDocument();
            valueTobeUpdate.append("sum", addObject);
            BsonDocument mainProjection=new BsonDocument();
            mainProjection.append("$project", valueTobeUpdate);
            List<BsonDocument> pipeline=new ArrayList<BsonDocument>();
            pipeline.add(mainProjection);
            AggregateIterable<Document> iterable = refCollection.aggregate(pipeline);

如何将$ match运算符添加到上面显示的代码?有什么建议吗?

1 个答案:

答案 0 :(得分:2)

$and 管道中的 $match 运算符不是必需的,因为您可以通过仅指定逗号来隐式执行AND操作表达列表。

汇总管道可以重组为:

Mongo shell:

/*
    MONGO SHELL: 
    var pipeline = [ 
        { 
            "$match": { "_id": { "$gte": 1, "$lte": 2 } } // or  "$match": { "_id": { "$in": [1,2] } }
        }, 
        { 
            "$project": { 
                "final": 1, 
                "midterm": 1, 
                "examTotal": { 
                    "$add": [ "$final", "$midterm", { "$ifNull": [ "$extraMarks", 10 ] } ] 
                } 
            } 
        } 
    ];
    db.students.aggregate(pipeline);

*/

Java实施:

public class JavaAggregation {
    public static void main(String args[]) throws UnknownHostException {

        MongoClient mongo = new MongoClient();
        DB db = mongo.getDB("test");

        DBCollection coll = db.getCollection("students");

        // create the pipeline operations, first with the $match
        DBObject match = new BasicDBObject("$match",
                            new BasicDBObject("_id",
                                new BasicDBObject("$gte", 1).append("$lt", 2)
                            )
                        );

        // build the $project operations
        BasicDBList coalesce = new BasicDBList();
        coalesce.add("$extraMarks");
        coalesce.add(10)
        DBObject ifNullClause = new BasicDBObject("$ifNull", coalesce);  

        BasicDBList addition = new BasicDBList();
        addition.add("$final");
        addition.add("$midterm");
        addition.add(ifNullClause);

        DBObject examTotal = new BasicDBObject("$add", addition);

        DBObject fields = new BasicDBObject("final", 1);
        fields.put("midterm", 1);
        fields.put("examTotal", examTotal);

        DBObject project = new BasicDBObject("$project", fields);
        List<DBObject> pipeline = Arrays.asList(match, project);

        AggregationOutput output = coll.aggregate(pipeline);

        for (DBObject result : output.results()) {
            System.out.println(result);
        }
    }
}