【问题标题】:How to give Multiple Projections in MongoDB java using Aggregation Framework如何使用聚合框架在 MongoDB java 中给出多个投影
【发布时间】:2016-02-08 13:43:54
【问题描述】:

我有三个文件

{ "_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 的人获取此信息

所以我将代码修改为以下内容: 查询2:

 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 运算符?有什么建议吗?

【问题讨论】:

  • 你为什么不pipeline.add比赛舞台,就像你加了投影舞台一样?

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


【解决方案1】:

$match 管道中的 $and 运算符并不是真正需要的,因为您可以通过指定逗号分隔的表达式列表来隐式执行 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);
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多