javascript - 如何使用猫鼬聚合根据字段获取两个匹配文档的总和
问题描述
我有两个集合“员工”、“办公室”
我试图找出每个包含办公室代码的区域有多少员工。但同一地区可能有不止一个办公室。
这就是我的 Office 文档的外观
[
{
_id: "5b7d0f77e231b6b530b0ee5a",
code: "OB123456",
city: "Canmore"
// some other fields
},
{
_id: "5b7d0f77e531b6b530b0ee5b",
code: "OB858758",
city: "Vancouver"
},
{
_id: "5b7d0f77e531b6b530b0ee5d",
code: "EE858758",
city: "Vancouver"
},
]
这就是我的员工文件的样子
[
{
_id: "5b7d0f77e531b6b530b0edda",
name: 'Charlie',
office: {
code: 'OB123456'
// some other fields
}
},
{
_id: "5b7d0f73e531b6b530b0ee5b",
name: 'Bill',
office: {
code: 'EE858758'
}
},
{
_id: "5b7d0f77e531b6b530b0ee5n",
name: 'Echo',
office: {
code: 'OB123456'
}
},
];
我正在研究猫鼬聚合体,并且只尝试过
await Employee.aggregate([
{
$lookup: {
from: 'offices',
localField: 'office.code',
foreignField: 'code',
as: 'officeCode'
},
$group: {
_id: 'officeCode.city',
count: { $sum: 1 }
}
}
]);
这肯定行不通,我尝试阅读一些聚合文档,但无法想出如何完成此操作的好主意
提前感谢您的任何建议或意见。
我正在寻找的样本输出
{
"Vancouver": 1,
"Canmore": 2
}
解决方案
您必须从办公室而不是员工开始,因此您可以为每个区域(城市)创建一个代码列表,然后查找与您的员工进行映射。
db.office.aggregate([
{
$group: {
_id: "$city",
codes: {
$addToSet: "$code"
}
}
},
{
$lookup: {
from: "employee",
localField: "codes",
foreignField: "office.code",
as: "employees"
},
},
{
$group: {
_id: null,
data: {
$push: {
k: "$_id",
v: {
$size: "$employees"
}
}
}
}
},
{
$replaceRoot: {
newRoot: {
"$arrayToObject": "$data"
}
}
}
])
最后两个阶段仅用于格式化结果,如预期输出中所述。
推荐阅读
- google-cloud-platform - 由于缺少 CMake,Google Cloud Build 失败
- firebase-realtime-database - 在 iOS 中的节点上更新值后,Firebase 节点意外删除自身
- javascript - 使用 background-image 属性制作动画是否最佳?
- python - 如何在终端外访问烧瓶应用程序?
- session-cookies - 负载均衡器的 Marklogic Rest API 事务问题
- python - AttributeError:'NoneType' 对象在基于空间分隔时没有属性'split'
- excel - 如何在excel中删除多个工作表中的重复值和原始值
- ruby-on-rails - 在 Rails+Passenger 服务器中启动应用程序作为服务
- raid - RAID5 故障,所有超级块丢失,无法重新组装阵列
- linux - 如何解决 xfce 中的 cytoscape 安装错误?