在指定条件下汇总数组内对象的值

时间:2019-11-15 09:21:53

标签: mongodb mongodb-query

我收集了超过30万个文档,如下所示:

{ 
    "_id" : 1, 
    "emp_no" : 10031, 
    "salaries" : [
        {
            "salary" : 40000, 
            "from_date" : "1991-09-01", 
            "to_date" : "1992-08-31"
        }, 
        {
            "salary" : 40859, 
            "from_date" : "1992-08-31", 
            "to_date" : "1993-08-31"
        }, 
        {
            "salary" : 41881, 
            "from_date" : "1993-08-31", 
            "to_date" : "1994-08-31"
        }, 
        {
            "salary" : 44191, 
            "from_date" : "1994-08-31", 
            "to_date" : "1995-08-31"
        }, 
        {
            "salary" : 47202, 
            "from_date" : "1995-08-31", 
            "to_date" : "1996-08-30"
        }, 
        {
            "salary" : 47606, 
            "from_date" : "1996-08-30", 
            "to_date" : "1997-08-30"
        }, 
        {
            "salary" : 50810, 
            "from_date" : "1997-08-30", 
            "to_date" : "1998-08-30"
        }, 
        {
            "salary" : 52675, 
            "from_date" : "1998-08-30", 
            "to_date" : "1999-08-30"
        }, 
        {
            "salary" : 54177, 
            "from_date" : "1999-08-30", 
            "to_date" : "2000-08-29"
        }, 
        {
            "salary" : 53873, 
            "from_date" : "2000-08-29", 
            "to_date" : "2001-08-29"
        }, 
        {
            "salary" : 56689, 
            "from_date" : "2001-08-29", 
            "to_date" : "9999-01-01"
        }
    ]
}
{ 
    "_id" : 2, 
    "emp_no" : 10033, 
    "salaries" : [
        {
            "salary" : 51258, 
            "from_date" : "1987-03-18", 
            "to_date" : "1988-03-17"
        }, 
        {
            "salary" : 54972, 
            "from_date" : "1988-03-17", 
            "to_date" : "1989-03-17"
        }, 
        {
            "salary" : 55410, 
            "from_date" : "1989-03-17", 
            "to_date" : "1990-03-17"
        }, 
        {
            "salary" : 56095, 
            "from_date" : "1990-03-17", 
            "to_date" : "1991-03-17"
        }, 
        {
            "salary" : 56038, 
            "from_date" : "1991-03-17", 
            "to_date" : "1992-03-16"
        }, 
        {
            "salary" : 57712, 
            "from_date" : "1992-03-16", 
            "to_date" : "1993-03-16"
        }, 
        {
            "salary" : 60433, 
            "from_date" : "1993-03-16", 
            "to_date" : "1993-03-24"
        }
    ]
}
{ 
    "_id" : 3, 
    "emp_no" : 10035, 
    "salaries" : [
        {
            "salary" : 41538, 
            "from_date" : "1988-09-05", 
            "to_date" : "1989-09-05"
        }, 
        {
            "salary" : 45131, 
            "from_date" : "1989-09-05", 
            "to_date" : "1990-09-05"
        }, 
        {
            "salary" : 45629, 
            "from_date" : "1990-09-05", 
            "to_date" : "1991-09-05"
        }, 
        {
            "salary" : 48360, 
            "from_date" : "1991-09-05", 
            "to_date" : "1992-09-04"
        }, 
        {
            "salary" : 50664, 
            "from_date" : "1992-09-04", 
            "to_date" : "1993-09-04"
        }, 
        {
            "salary" : 53060, 
            "from_date" : "1993-09-04", 
            "to_date" : "1994-09-04"
        }, 
        {
            "salary" : 56640, 
            "from_date" : "1994-09-04", 
            "to_date" : "1995-09-04"
        }, 
        {
            "salary" : 57621, 
            "from_date" : "1995-09-04", 
            "to_date" : "1996-09-03"
        }, 
        {
            "salary" : 59291, 
            "from_date" : "1996-09-03", 
            "to_date" : "1997-09-03"
        }, 
        {
            "salary" : 61793, 
            "from_date" : "1997-09-03", 
            "to_date" : "1998-09-03"
        }, 
        {
            "salary" : 62285, 
            "from_date" : "1998-09-03", 
            "to_date" : "1999-09-03"
        }, 
        {
            "salary" : 65332, 
            "from_date" : "1999-09-03", 
            "to_date" : "2000-09-02"
        }, 
        {
            "salary" : 66584, 
            "from_date" : "2000-09-02", 
            "to_date" : "2001-09-02"
        }, 
        {
            "salary" : 68755, 
            "from_date" : "2001-09-02", 
            "to_date" : "9999-01-01"
        }
    ]
}

工资是每年的。我需要知道 在特定年份中所有员工的收入是多少。 是否有一种无需使用$unwind运算符的方法?因为如果我展开所有这些数组,将有超过280万个重复项,而我宁愿避免处理其中的许多重复项。
到目前为止,我想到了以下查询:

db.getCollection("tmp").find(
    {salaries: 
        {$elemMatch: 
            { "to_date": { $gte: "1989-01-01" } ,  
             "to_date": { $lte: "1989-01-01" } } 
        }
    }
)

但这只返回整个文档,我不知道如何对匹配的salary字段的值求和。

0 个答案:

没有答案