【发布时间】:2020-02-03 14:39:55
【问题描述】:
给定以下数据:
let tasks = [
{
_id: 1,
task_number: 1,
description: 'Clean Bathroom',
customer: 'Walmart',
users_worked: [
{user: 'Jonny', hours: 1},
{user: 'Cindy', hours: 1}
],
supplies_used: [
{item_code: 'LD4949', description: 'Liquid Detergent', quantity: 1}
]
},
{
_id: 2,
task_number: 2,
description: 'Stock Cheeses',
customer: 'Walmart',
users_worked: [
{user: 'Mark', hours: 3.0},
{user: 'Shelby', hours: 2.0}
],
supplies_used: []
}
];
假设我想以列表格式显示一个表格:
task_number | description | customer | users | users_worked.hours (sum) | supplies_used.quantity (sum)
----------------------------------------------------------------------------------------------
1 | 'Clean Bathroom' | 'Walmart' | 'Jonny, Cindy' | 2 | 1
2 | 'Stock Cheeses' | 'Walmart' | 'Mark, Shelby' | 5 | 0
聚合:
[
{
"unwind: {
"path": "$users_worked",
"preserveNullAndEmptyArrays": true
}
},
{
"unwind: {
"path": "$supplies_used",
"preserveNullAndEmptyArrays": true
}
},
{
$group: {
_id: "$_id",
task_number: {
$first: "$task_number"
},
description: {
$first: "$description"
},
customer: {
$first: "$customer"
},
users: {
$push: "$users_worked.user"
},
users_worked: {
$sum: "$users_worked.hours"
},
supplies_used: {
$sum: "$supplies_used.quantity"
}
}
]
问题是我需要$unwind 两个数组(users_worked 和supplies_used),这最终会扭曲我的结果(笛卡尔积)。由于任务 #1 在 users_worked 数组中有 2 个元素,它会使我的 supplies_used 计数变为 2。
这是一个简单的例子,可能有很多数组,每个元素越多,数据的倾斜度就越大。
有没有一种聚合方法可以分别展开多个数组,这样它们就不会相互倾斜?我看过一个创建 1 个组合对象的示例,其中只有 1 个展开源。似乎不明白如何做我想做的事。
* 编辑 *
我看到您可以使用$zip mongo aggregate 命令将多个数组组合成一个数组。这是一个好的开始:
arrays: {
$map: {
input: {
$zip: {
inputs: [
'$users_worked',
'$supplies_used'
],
}
},
as: 'zipped',
in: {
users_worked: {
$arrayElemAt: [
'$$zipped',
0
]
},
supplies_used: {
$arrayElemAt: [
'$$zipped',
1
]
}
如果我有一个数组数组,我该如何使用这个$zip 命令。例如:
let tasks = [
{
_id: 1,
task_number: 1,
description: 'Clean Bathroom',
customer: 'Walmart',
users_worked: [
{user: 'Jonny', hours: 1},
{user: 'Cindy', hours: 1}
],
supplies_used: [
{item_code: 'LD4949', description: 'Liquid Detergent', quantity: 1}
],
invoices: [
{
invoicable: true,
items: [
{item_code: 'LD4949', price: 39.99, quantity: 1, total: 39.99},
{item_code: 'Hours', price: 50.00, quantity: 2, total: 100.00}
]
}
]
},
{
_id: 2,
task_number: 2,
description: 'Stock Cheeses',
customer: 'Walmart',
users_worked: [
{user: 'Mark', hours: 3.0},
{user: 'Shelby', hours: 2.0}
],
supplies_used: [],
invoices: []
}
];
我想在我的列表中包含 invoices.items.total 的总和。
【问题讨论】:
标签: mongodb aggregation-framework aggregation