Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有Java聚合驱动程序的MongoDB查询和Java代码_Mongodb_Aggregation Framework - Fatal编程技术网

带有Java聚合驱动程序的MongoDB查询和Java代码

带有Java聚合驱动程序的MongoDB查询和Java代码,mongodb,aggregation-framework,Mongodb,Aggregation Framework,有人能帮我告诉我下面对多个帐户的查询是否正确,这样我就可以从我的集合中一次性获取多个帐户的“mt”的总和吗 我尝试了以下查询: db.getCollection('XYZ').aggregate([ {$match: { $and:[{ $or: [ {"try.Ind":"A","_id.a":"BB","_id.b":"HXYZ","_id.

有人能帮我告诉我下面对多个帐户的查询是否正确,这样我就可以从我的集合中一次性获取多个帐户的“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"

                }
                        }
                    }
                }
            } 
    ])
MyCollection数据:

    /* 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

等等

输出将是下面的lyk

{
    {
            "_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代码,特别是匹配条件


提前感谢

你的问题是正确的!可能重复的