使用Java聚合驱动程序的MongoDB查询和Java代码

时间:2018-08-02 17:39:02

标签: mongodb aggregation-framework

有人可以帮我说一下下面对多个帐户的查询是否正确,以便我可以一次从我的收藏夹中获取多个帐户的“ mt”总和吗?

我尝试了如下查询,

    db.getCollection('XYZ').aggregate([
            {$match: {
                $and:[{
                    $or: [
                        {"try.Ind":"A","_id.a":"BB","_id.b":"HXYZ","_id.c":"12345","_id.d":"FG","_id.e": new Date("2013-10-01T00:00:00.000Z")},
                        {"try.Ind":"A","_id.a":"AB","_id.b":"BBBT","_id.c":"23457","_id.d":"DA","_id.e":new Date("2013-10-01T00:00:00.000Z")}


                    ]
                }]
               }
            },
            {$project:
                {
                  "try": 1,
                    total:{
                      $sum:{
                        $let: {
                          vars: {
                              array:{
                                    $filter:{
                          input:"$try",
                          cond:{$and:[{$eq:["$$this.Ind","A"]},{$gt:["$$this.mt",0.0]}]}
                    }
                }},
                        "in":"$$array.mt"

                }
                        }
                    }
                }
            } 
    ])

我的收藏数据:

    /* 1 */
{
    "_id" : {
        "a" : "NA",
        "b" : "HXYZ",
        "c" : "12345",
        "d" : "CA",
        "e" : "2018-03-09",
    },
    "try" : [ 
        {
            "Ind" : "A",
            "mt" : 2.0,

        }, 
        {
            "Ind" : "B",
            "mt" : 3.0,

        }, 
        {
            "Ind" : "A",
            "mt" : 4.0,

        }, 
        {
            "Ind" : "B",
            "mt" : 5.0,

        }, 
        {
            "Ind" : "A",
            "mt" : 6.0,

        }, 
        {
            "Ind" : "B",
            "mt" : 7.0,

        }
    ]
}

/* 2*/
{
    "_id" : {
        "a" : "AA",
        "b" : "ACDE",
        "c" : "45678",
        "d" : "AB",
        "e" : "2018-03-09",
    },
    "try" : [ 
        {

            "Ind" : "A",
            "mt" : 2.0,

        }, 
        {

            "Ind" : "B",
            "mt" : 3.0,

        }, 
        {

            "Ind" : "A",
            "mt" : 4.0,

        }, 
        {

            "Ind" : "B",
            "mt" : 5.0,

        }, 
        {

            "Ind" : "A",
            "mt" : 6.0,

        }, 
        {

            "Ind" : "B",
            "mt" : 7.0,

        }
    ]
}

/* 3 */
{
    "_id" : {
        "a" : "BB",
        "b" : "BBBY",
        "c" : "89012",
        "d" : "BA",
        "e" : "2004-02-12",
    },
    "try" : [ 
        {

            "Ind" : "A",
            "mt" : 2.0,

        }, 
        {

            "Ind" : "B",
            "mt" : 3.0,

        }, 
        {

            "Ind" : "A",
            "mt" : 4.0,

        }, 
        {

            "Ind" : "B",
            "mt" : 5.0,

        }, 
        {

            "Ind" : "A",
            "mt" : 6.0,

        }, 
        {

            "Ind" : "B",
            "mt" : 7.0,

        }
    ]
}

预期输出:

如果Ind =“ A”和mt> 0.0

对于帐户12345,

2.0 + 4.0 + 6.0 = 12

对于89012帐户,

2.0 + 4.0 + 6.0 = 12

以此类推...

输出将在下面出现,

{
    {
            "_id": {
            "a" : "NA",
            "b" : "HXYZ",
            "c" : "12345",
            "d" : "CA",
            "e" : "2018-03-09",
            },
            "total": 12
    },{     "_id": {
                "a" : "BB",
                "b" : "BBBY",
                "c" : "89012",
                "d" : "BA",
                "e" : "2004-02-12",
            },
            "total" :12

    }
}

如果上述查询适用于多个帐户,请从集合中一次性获得所有帐户的总和,请纠正我?

对于上述查询,我​​还需要使用聚合的相应Java代码,尤其是匹配条件。

提前谢谢!

0 个答案:

没有答案