- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正致力于在 MongoDB 中开发一个管道,它将每个州的公司的所有 number_of_employees 相加,并打印出每个州的信息。
集合结构的基本原理如下所示:
"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" }
...
最佳答案
我相信这就是您正在寻找的管道:
db.research.aggregate([
{
"$match": {
"offices.country_code": "USA"
}
},
{
"$unwind": "$offices"
},
{
"$project": {
"number_of_employees": "$number_of_employees",
"state_code": "$offices.state_code",
"_id": 0
}
},
{
"$group": {
"_id": "$state_code",
"total_employees": {
"$sum": "$number_of_employees"
}
}
},
{
"$project": {
"Total_Employees": "$total_employees",
"State": "$_id",
"_id": 0
}
}
])
你可以看到它在这里工作:https://mongoplayground.net/p/XUA_e9Y4Fq_
如果有人感兴趣,这里是生成上述查询的 c# 代码:
using MongoDB.Entities;
using System.Linq;
namespace StackOverflow
{
public class Program
{
public class company : Entity
{
public int number_of_employees { get; set; }
public office[] offices { get; set; }
}
public class office
{
public string state_code { get; set; }
public string country_code { get; set; }
}
private static void Main(string[] args)
{
new DB("test");
(new[]
{
new company{
number_of_employees = 100,
offices = new[]
{
new office
{
state_code = "NY",
country_code = "USA"
}
}
},
new company{
number_of_employees = 100,
offices = new[]
{
new office
{
state_code = "NY",
country_code = "USA"
},
new office
{
state_code = "LA",
country_code = "USA"
}
}
}
}).Save();
var result = DB.Queryable<company>()
.Where(c => c.offices.Any(o => o.country_code == "USA"))
.SelectMany(c => c.offices, (c, o) => new { c.number_of_employees, o.state_code })
.GroupBy(c => c.state_code)
.Select(g => new
{
Total_Employees = g.Sum(c => c.number_of_employees),
State = g.Key
})
.ToList();
}
}
}
关于mongodb - 使用 unwind 创建单独的文档后如何计算值的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57245994/
我有一个从 A View Controller 到 B View Controller 的 unwind segue。 在B中做了一个网络操作,操作完成后,响应会显示在A View Controlle
我刚刚意识到,在当前的 Xcode 8 beta 6 中,当我将展开操作连接到例如模态呈现的 View Controller 中的条形按钮项目以展开模态呈现转场并呈现 View Controller
我是 mongodb 聚合的新手。我的 mongo 文档有很多数组。我需要将其导出为平面文件。为此,我需要构建它。我尝试了以下聚合: [ {$unwind : "$items" }, {$unwind
我正在尝试使用 De Bruijn 指数定义 lambda 演算项。我在 OS X 上使用 swi prolog。 如果我使用自然数的 zero|successor 表示,我可以交互式地完成部分指定的
我是 mongodb 聚合的新手。我的 mongo 文档有很多数组。我需要将其导出为平面文件。为此,我需要构建它。我尝试了以下聚合: [ {$unwind : "$items" }, {$unwind
有了这些数据: { "_id" : ObjectId("576948b4999274493425c08a"), "virustotal" : { "scan_id" :
我想要$unwind 2 个字段,school 和home。数据库结构是这样的; { "id" : 1, "school" : [ { "pat
考虑这组测试结果: [{ _id: ObjectId(...), name: "Test1", acts: [ { name: "act1",
我想要 go 中的结果,因为 mongo shell 提供给我。 在 mongo shell 中数据是这样的: db.user.aggregate([{$unwind:"$user"}]).prett
这个问题 - Is it possible to get a slice of a slice in Mongo?涵盖了如何在 Mongo 中获取切片。简而言之,使用聚合链来$unwind、$skip
我有一个用户集合,其中每个文档具有以下结构: { "_id": "", "login": "xxx", "solved": [ { "problem": "",
在 MongoDB 聚合框架中,我希望在对象(即 JSON 集合)上使用 $unwind 运算符。看起来不像是 possible ,有解决方法吗?有计划实现吗? 例如,从聚合 documentatio
假设我有一个名为节点的参数: "nodes": [ { "name": "John", "age": 18.0, "label": "Person",
我的文档有一个字段symptoms,它是一个数组的数组。 我想$unwind它并应用$addToSet删除重复项, 然后只需更新文档即可。怎么做 ? 文档 "symptoms": [ [
我正在编写一个非常简单的应用程序,它有两个 View Controller - FirstVC 和 SecondVC。在 FirstVC 上,我加载了一个视频 (apples.mp4),它在后台(在我
我在想,在聚合管道中为嵌套数组的文档使用 $unwind 运算符是否会以与数组中项目的顺序相同的顺序返回解构的文档。例子:假设我有以下文件 { "_id" : 1, "item" : "foo", v
我有一份教育机构文件,看起来像这样: { name: ..., addresses: [...], courses: [ {name: ... , duration: ..., tags[...]}
使用 Storyboard这非常容易。您只需将操作拖至“退出”即可。但我应该如何从我的代码中调用它? 最佳答案 创建手动转场(ctrl-从文件所有者拖动到退出), 在绿色 EXIT 按钮下方的左侧 C
iOS 6 和 Xcode 4.5 有一个称为“Unwind Segue”的新功能: Unwind segues can allow transitioning to existing instanc
我有一个 Storyboard,它有一个起始 View Controller ,它呈现其他模态视图 Controller 。 假设 View Controller A 是起始 View Control
我是一名优秀的程序员,十分优秀!