[英]$sum aggregation in mongodb nodej driver
集合 1:管理员
{
"_id" : ObjectId("5e27fd3da42d441fe8a89580"),
"mappedcustomers": [
ObjectId("5e2555783405363bc4bf86c5"),
ObjectId("5e2555783405363bc4bf86c0"),
ObjectId("5e2555783405363bc4bf86c4")
],
"phoneNo" : 9897654358,
"name" : "acdbcs"
}
集合 2:productOrders
Tt有很多文件,我们只关心:
"productOrderedForDate" : ISODate("2020-02-04T18:30:00Z")
[明天的订单]。"productOrderedForDate" : ISODate("2020-02-28T18:30:00Z")
[上周订单][
{
"_id": ObjectId("5e27f998a42d441fe8a8957f"),
"authorized": false,
"orderCreatedBy": ObjectId("5e2555783405363bc4bf86c4"), // one of the mappedCustomer
"productOrderedForDate": ISODate("2020-02-04T18:30:00Z"),// tomorrow Order
"order": [{
"_id": ObjectId("5e26be2cc13b7149d0a95110"),
"productName": "Cups",
"productCode": "CICE1",
"size R": 21,
"size L": 16
},
{
"_id": ObjectId("5e26be2cc13b7149d0a9510f"),
"productName": "Bottles",
"productCode": "BTCE1",
"size R": 12,
"size L": 3
}]
},
{
"_id": ObjectId("5e26be2cc13b7149d0b90752b"),
"authorized": false,
"orderCreatedBy": ObjectId("5e2555783405363bc4bf86c0"),// another mappedCustomer
"productOrderedForDate": ISODate("2020-02-04T18:30:00Z"),// tomorrow Order
"order": [{
"_id": ObjectId("5e26be2cc13b7149d0a87230"),
"productName": "Cups",
"productCode": "CICE1",
"size R": 9,
"size L": 7
},
{
"_id": ObjectId("5e26be2cc13b7149d0a8560e"),
"productName": "Bottles",
"productCode": "BTCE1",
"size R": 3,
"size L": 11
}]
},
{
"_id": ObjectId("5e26be2cc13b7149d0b9876f"),
"authorized": true,
"orderCreatedBy": ObjectId("5e2555783405363bc4bf86c4"), // one of the mappedCustomer
"productOrderedForDate": ISODate("2020-01-28T18:30:00Z"),// lastWeek order
"order": [{
"_id": ObjectId("5e26be2cc13b7149d0a54220"),
"productName": "Cups",
"productCode": "CICE1",
"size R": 2,
"size L": 6
},
{
"_id": ObjectId("5e26be2cc13b7149d0a6520e"),
"productName": "Bottles",
"productCode": "BTCE1",
"size R": 8,
"size L": 16
}]
},
{
"_id": ObjectId("5e78f998a42d441fe898765d"),
"authorized": true,
"orderCreatedBy": ObjectId("5e2555783405363bc4bf86c0"), // another mappedCustomer
"productOrderedForDate": ISODate("2020-01-28T18:30:00Z"),// lastWeek order
"order": [{
"_id": ObjectId("5e26be2cc13b7149d0a87230"),
"productName": "Cups",
"productCode": "CICE1",
"size R": 26,
"size L": 19
},
{
"_id": ObjectId("5e26be2cc13b7149d0a8560f"),
"productName": "Bottles",
"productCode": "BTCE1",
"size R": 4,
"size L": 5
}]
}
]
这是我尝试过的并且能够展开所有映射的客户,因此我能够在下面的订单集合中找到它们创建的订单是聚合管道
db.admin.aggregate([
{
$match: {
_id: ObjectId("5e27fd3da42d441fe8a89580")
}
},
{
$lookup:
{
from: 'admin',
localField: 'mappedCustomers',
foreignField: '_id',
as: 'mappedCustomers'
}
},
{
$unwind: '$mappedCustomers'
},
{
$replaceRoot: {newRoot: "$mappedCustomers"}
},
{
$lookup:
{
from: "orders",
let: {mappedCustomersId: "$_id"},
pipeline: [
{
$match: {
$expr: {$eq: ["$orderCreatedBy", "$$mappedCustomersId"]},
'$or': [
{
'orderCreatedOn': ISODate("2020-02-04T18:30:00Z")
}, {
'orderCreatedOn': ISODate("2020-01-28T18:30:00Z")
}]
}
}],
as: "orders"
}
}, {
$unwind: "orders"
}
])
我的问题是,我需要针对明天日期和上周日期在该管理员下映射的productCode
所有映射的mappedCustomers
的所有size R
和size L
的总和,即
预期输出:
{
orders : [
{
"productOrderedForDate": ISODate("2020-02-04T18:30:00Z"),
"productName": "Cups",
"productCode": "CICE1",
"size R": 30,
"size L": 23,
"lastWeek": [{
"productOrderedForDate": ISODate("2020-01-28T18:30:00Z"),
"size R": 28,
"size L": 25,
}]
}, {
"productOrderedForDate": ISODate("2020-02-04T18:30:00Z"),
"productName": "Bottles",
"productCode": "BTCE1",
"size R": 15,
"size L": 14,
"lastWeek": [{
"productOrderedForDate": ISODate("2020-01-28T18:30:00Z"),
"size R": 12,
"size L": 21,
}]
}
]
}
回顾一下: 1. 我将从req.body
获取管理员id
。 2. 我会发现所有的客户都映射到了mappedCustomers
。 3. 我将从orders
集合中查找mappedCustomers
为所需日期创建的订单。 4. 我需要将所有size R
和size L
分组。
我设法做到了 1,2,3,但我无法为 4 和 5 产生所需的结果。请看看它并告诉我这是否可以实现。
我已经看过这篇文章,但我无法让它工作。
尝试这个:
db.admin.aggregate([
{
$match: {
_id: ObjectId("5e27fd3da42d441fe8a89580")
}
},
{
$lookup: {
from: "admin",
localField: "mappedcustomers",
foreignField: "_id",
as: "mappedcustomers"
}
},
{
$unwind: "$mappedcustomers"
},
{
$replaceRoot: {
newRoot: "$mappedcustomers"
}
},
{
$lookup: {
from: "orders",
let: {
mappedCustomersId: "$_id"
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$orderCreatedBy",
"$$mappedCustomersId"
]
},
"$or": [
{
"productOrderedForDate": ISODate("2020-02-04T18:30:00Z")
},
{
"productOrderedForDate": ISODate("2020-01-28T18:30:00Z")
}
]
}
}
],
as: "orders"
}
},
{
$unwind: "$orders"
},
{
$unwind: "$orders.order"
},
{
$group: {
_id: "$orders.order.productCode",
orders: {
$push: {
productOrderedForDate: "$orders.productOrderedForDate",
productName: "$orders.order.productName",
productCode: "$orders.order.productCode",
"size R": "$orders.order.size R",
"size L": "$orders.order.size L"
}
}
}
},
{
$project: {
thisweek: {
$reduce: {
input: {
$filter: {
input: "$orders",
cond: {
$eq: [
"$$this.productOrderedForDate",
ISODate("2020-02-04T18:30:00Z")
]
}
}
},
initialValue: {
"size R": 0,
"size L": 0
},
in: {
productOrderedForDate: "$$this.productOrderedForDate",
"productName": "$$this.productName",
"productCode": "$$this.productCode",
"size R": {
$add: [
"$$value.size R",
"$$this.size R"
]
},
"size L": {
$add: [
"$$value.size L",
"$$this.size L"
]
}
}
}
},
lastWeek: {
$reduce: {
input: {
$filter: {
input: "$orders",
cond: {
$eq: [
"$$this.productOrderedForDate",
ISODate("2020-01-28T18:30:00Z")
]
}
}
},
initialValue: {
"size R": 0,
"size L": 0
},
in: {
productOrderedForDate: "$$this.productOrderedForDate",
"size R": {
$add: [
"$$value.size R",
"$$this.size R"
]
},
"size L": {
$add: [
"$$value.size L",
"$$this.size L"
]
}
}
}
}
}
},
{
$group: {
_id: null,
orders: {
$push: {
$mergeObjects: [
"$thisweek",
{
"lastWeek": [
"$lastWeek"
]
}
]
}
}
}
},
{
$unset: "_id"
}
])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.