Mongodb 使用展开创建单独文档后,如何计算值的总和?

Mongodb 使用展开创建单独文档后,如何计算值的总和?,mongodb,sum,pipeline,Mongodb,Sum,Pipeline,我正致力于在MongoDB开发一条管道,将各州公司的所有雇员人数相加,并打印出各州的信息 集合结构的基本原理如下所示: "name" : "AdventNet", "number_of_employees" : 600, "offices" : [ { "description" : "Headquarters", "address1" : "4900 Hopyard Rd.", "address2" : "Suit

我正致力于在MongoDB开发一条管道,将各州公司的所有雇员人数相加,并打印出各州的信息

集合结构的基本原理如下所示:

"name" : "AdventNet",
"number_of_employees" : 600,
"offices" : [
       {
           "description" : "Headquarters",
           "address1" : "4900 Hopyard Rd.",
           "address2" : "Suite 310",
           "zip_code" : "94588",
           "city" : "Pleasanton",
           "state_code" : "CA",
           "country_code" : "USA",
           "latitude" : 37.692934,
           "longitude" : -121.904945
        },
        {
            "description" : "",
            "address1" : "270 Lafayette Street",
            "address2" : "Suite 505",
            "zip_code" : "10012",
            "city" : "New York",
            "state_code" : "NY",
            "country_code" : "USA",
            "latitude" : 40.7237306,
            "longitude" : -73.9964312
        }
],
我已经尝试了几种不同的方法来获取最新的管道代码,但我仍然在努力使求和正确工作。我尝试了不同的分组结构,但总是出错

db.research.aggregate( [
    {$match : {"offices.country_code" : "USA"} }, 
    {$project: {State : "$offices.state_code"}}, 
    {$unwind: "$State"}, 
    { $group : {"_id" : "$State", "total_employees": {$sum : "$number_of_
employees"} }}, 
    {$project: {_id : 0, State: "$_id", total_employees: 1}}
])
预期结果:

{ "total_employees" : 1500, "State" : "SD" }
{ "total_employees" : 350, "State" : "WV" }
...
实际结果:

{ "total_employees" : 0, "State" : "SD" }
{ "total_employees" : 0, "State" : "WV" }
...

如果我说得对的话,你需要州政府的雇员总数。 然后,您只需更改组管道中的
total_employees:{$sum:1}

db.research.aggregate([
    {
        $unwind: "$offices"
    },
    {
        $match: {
            'offices.country_code': "USA"
        }
    },
    {
        $group: {
            _id: "$offices.state_code",
            total_employees: { $sum: 1 },
        }
    },
    { 
        $project: { 
            _id: 0, 
            State: "$_id", 
            total_employees: 1
        } 
    }
])

我相信这就是您正在寻找的管道:

db.research.aggregate([
{
“$match”:{
“办事处.国家/地区代码”:“美国”
}
},
{
“$unwind”:“$offices”
},
{
“$project”:{
“员工人数”:“员工人数”,
“州代码”:“$offices.state\u代码”,
“\u id”:0
}
},
{
“$group”:{
“\u id”:“$state\u code”,
“员工总数”:{
“$sum”:“$number\U员工”
}
}
},
{
“$project”:{
“员工总数”:“$Total_Employees”,
“状态”:“$\u id”,
“\u id”:0
}
}
])
您可以看到它在这里工作:

另外,如果有人感兴趣,下面是生成上述查询的c代码:

使用MongoDB.Entities;
使用System.Linq;
命名空间堆栈溢出
{
公共课程
{
上市公司:实体
{
公共整数{get;set;}
公共办公室[]办公室{get;set;}
}
公课办公室
{
公共字符串状态_代码{get;set;}
公共字符串国家代码{get;set;}
}
私有静态void Main(字符串[]args)
{
新DB(“测试”);
(新[]
{
新公司{
员工人数=100,
办公室=新[]
{
新办公室
{
国家代码=“NY”,
国家/地区代码=“美国”
}
}
},
新公司{
员工人数=100,
办公室=新[]
{
新办公室
{
国家代码=“NY”,
国家/地区代码=“美国”
},
新办公室
{
声明_code=“LA”,
国家/地区代码=“美国”
}
}
}
}).Save();
var result=DB.Queryable()
.Where(c=>c.offices.Any(o=>o.country_code==“USA”))
.SelectMany(c=>c.offices,(c,o)=>new{c.number\u员工,o.state\u代码})
.GroupBy(c=>c.state\u代码)
.选择(g=>new
{
员工总数=g.Sum(c=>c.员工人数),
状态=g.键
})
.ToList();
}
}

}
您在第二个
$project
阶段中丢失了
员工人数
字段。提到它,
{$project:{State:$offices.State_code],员工人数:1},