对我的数据进行一些聚合后,我留下了以下数据:
{
"_id" : {
"incomeAccountName" : "Kaffe/Te",
"type" : "REFUND"
},
"amounts" : [
[
{
"amount" : -78.0,
"currency" : "SEK"
}
],
[
{
"amount" : -34.0,
"currency" : "SEK"
}
]
],
"items" : [
{
"name" : "Latte mellan",
"amounts" : [
{
"amount" : -78.0,
"currency" : "SEK"
}
],
"taxAmounts" : [
{
"amount" : -8.35714626312256,
"currency" : "SEK"
}
],
"quantity" : -2.0,
"itemType" : "ARTICLE"
},
{
"name" : "Bryggkaffe",
"amounts" : [
{
"amount" : -34.0,
"currency" : "SEK"
}
],
"taxAmounts" : [
{
"amount" : -3.64285850524902,
"currency" : "SEK"
}
],
"quantity" : -1.0,
"itemType" : "ARTICLE"
}
]
}
现在我想减少金额:
amounts: [
{
"amount":-112,
"currency":"SEK"
}
]
即,计算金额/货币。请注意,我们可能有多种货币。使用聚合执行此操作而不影响其余数据集的最佳方法是什么?我正在使用mongo 3.4。
原始数据如下:
[{
"_id" : ObjectId("5ac1e43e24cc2974c7cda7a9"),
"alias" : "d89135d0-1067-46a9-92f3-44b1c4373612",
"billAlias" : "439d2e1c-0e7b-4521-960f-2bf87de90044",
"status" : "COMPLETE",
"type" : "PAYMENT",
"started" : ISODate("2018-04-02T08:05:17.937Z"),
"completed" : ISODate("2018-04-02T08:05:17.937Z"),
"merchant" : {
"merchantId" : "110b5def-3516-484a-9dc0-1fb9326488b0",
"terminalId" : "951321001",
"cashierId" : "598c22920cf27724adf035fa",
"placeAlias" : "20939de7-34bc-47a7-b04e-0df4864271ee",
"menuAlias" : "a3ac290d-a2b8-4e59-aea4-bbdf7106f2a2"
},
"currency" : "SEK",
"orderRows" : [
{
"name" : "Soppbuffé 98kr",
"itemType" : "ARTICLE",
"group" : "Varm Mat",
"incomeAccountName" : "Kall/varm mat",
"articleId" : "56aca26a-18ae-4079-8968-dd9cd3066168",
"extArticleId" : "1662",
"amount" : 98.0,
"amountInclDiscount" : 88.1999969482422,
"singleAmount" : 98.0,
"vatPercent" : 12.0,
"vatAmount" : 10.5000038146973,
"vatAmountInclDiscount" : 9.4500036239624,
"itemCount" : 1.0,
"ignoreDiscount" : false,
"cashierId" : "598c22920cf27724adf035fa",
"createdAt" : ISODate("2018-04-02T10:04:56.056Z")
},
{
"name" : "Studentrabatt 10%",
"itemType" : "DISCOUNT",
"group" : "RABATT",
"incomeAccountName" : "Rabatt/Discount",
"amount" : -9.80000019073486,
"amountInclDiscount" : -9.80000019073486,
"singleAmount" : -9.80000019073486,
"vatPercent" : 0.0,
"vatAmount" : -1.05000042915344,
"vatAmountInclDiscount" : -1.05000042915344,
"itemCount" : 1.0,
"ignoreDiscount" : false,
"discountPercent" : 10.0
}
]
},{
"_id" : ObjectId("5ac1e44224cc2974c7cda7ab"),
"alias" : "696ff805-0a43-4904-b1bc-f46847b4ab67",
"billAlias" : "dc49c822-6d74-4c70-80a7-aa5106666817",
"status" : "COMPLETE",
"type" : "PAYMENT",
"started" : ISODate("2018-04-02T08:05:21.998Z"),
"completed" : ISODate("2018-04-02T08:05:21.998Z"),
"merchant" : {
"merchantId" : "110b5def-3516-484a-9dc0-1fb9326488b0",
"terminalId" : "951321003",
"cashierId" : "598c22920cf27724adf035fa",
"placeAlias" : "20939de7-34bc-47a7-b04e-0df4864271ee",
"menuAlias" : "a3ac290d-a2b8-4e59-aea4-bbdf7106f2a2"
},
"currency" : "SEK",
"orderRows" : [
{
"name" : "Soppbuffé 98kr",
"itemType" : "ARTICLE",
"group" : "Varm Mat",
"incomeAccountName" : "Kall/varm mat",
"articleId" : "56aca26a-18ae-4079-8968-dd9cd3066168",
"extArticleId" : "1662",
"amount" : 98.0,
"amountInclDiscount" : 88.1999969482422,
"singleAmount" : 98.0,
"vatPercent" : 12.0,
"vatAmount" : 10.5000038146973,
"vatAmountInclDiscount" : 9.4500036239624,
"itemCount" : 1.0,
"ignoreDiscount" : false,
"cashierId" : "598c22920cf27724adf035fa",
"createdAt" : ISODate("2018-04-02T10:05:05.005Z")
},
{
"name" : "Studentrabatt 10%",
"itemType" : "DISCOUNT",
"group" : "RABATT",
"incomeAccountName" : "Rabatt/Discount",
"amount" : -9.80000019073486,
"amountInclDiscount" : -9.80000019073486,
"singleAmount" : -9.80000019073486,
"vatPercent" : 0.0,
"vatAmount" : -1.05000042915344,
"vatAmountInclDiscount" : -1.05000042915344,
"itemCount" : 1.0,
"ignoreDiscount" : false,
"discountPercent" : 10.0
}
]
},{
"_id" : ObjectId("5ac1e4a424cc2974c7cda7bd"),
"alias" : "f14f13a5-6f16-41ef-ac57-a511f394bf32",
"billAlias" : "04522348-d48d-4273-a511-09035666ced9",
"status" : "COMPLETE",
"type" : "PAYMENT",
"started" : ISODate("2018-04-02T08:07:00.618Z"),
"completed" : ISODate("2018-04-02T08:07:00.618Z"),
"merchant" : {
"merchantId" : "110b5def-3516-484a-9dc0-1fb9326488b0",
"terminalId" : "951321001",
"cashierId" : "598c22920cf27724adf035fa",
"placeAlias" : "20939de7-34bc-47a7-b04e-0df4864271ee",
"menuAlias" : "a3ac290d-a2b8-4e59-aea4-bbdf7106f2a2"
},
"currency" : "SEK",
"orderRows" : [
{
"name" : "Chévre Honey",
"itemType" : "ARTICLE",
"group" : "Kall Mat",
"incomeAccountName" : "Kall/varm mat",
"articleId" : "d8e49d26-30b8-4237-96ef-07bea2961537",
"extArticleId" : "1636",
"amount" : 99.0,
"amountInclDiscount" : 89.0999984741211,
"singleAmount" : 99.0,
"vatPercent" : 12.0,
"vatAmount" : 10.6071472167969,
"vatAmountInclDiscount" : 9.54643249511719,
"itemCount" : 1.0,
"ignoreDiscount" : false,
"cashierId" : "598c22920cf27724adf035fa",
"createdAt" : ISODate("2018-04-02T10:05:59.059Z")
},
{
"name" : "Bryggkaffe",
"itemType" : "ARTICLE",
"group" : "Varma drycker",
"incomeAccountName" : "Kaffe/Te",
"articleId" : "36672f69-6ac5-419e-a195-b3b0fcdf63b6",
"extArticleId" : "1608",
"amount" : 34.0,
"amountInclDiscount" : 30.6000003814697,
"singleAmount" : 34.0,
"vatPercent" : 12.0,
"vatAmount" : 3.64285850524902,
"vatAmountInclDiscount" : 3.27857279777527,
"itemCount" : 1.0,
"ignoreDiscount" : false,
"cashierId" : "598c22920cf27724adf035fa",
"createdAt" : ISODate("2018-04-02T10:06:23.023Z")
},
{
"name" : "Studentrabatt 10%",
"itemType" : "DISCOUNT",
"group" : "RABATT",
"incomeAccountName" : "Rabatt/Discount",
"amount" : -13.3000001907349,
"amountInclDiscount" : -13.3000001907349,
"singleAmount" : -13.3000001907349,
"vatPercent" : 0.0,
"vatAmount" : -1.42500054836273,
"vatAmountInclDiscount" : -1.42500054836273,
"itemCount" : 1.0,
"ignoreDiscount" : false,
"discountPercent" : 10.0
}
]
}]
我当前的聚合管道如下:
db.getCollection('orders').aggregate([
{$match: {"merchant.merchantId": "110b5def-3516-484a-9dc0-1fb9326488b0", status: "COMPLETE",completed : {$gte: ISODate("2018-04-02T00:46:12.331Z"), $lte: ISODate("2018-04-12T10:46:12.331Z")}}},
{$addFields: {"orderRows.discountName": {$arrayElemAt: [ {$filter: {input: "$orderRows",as: "orderRow",cond: { $eq:['$$orderRow.itemType','DISCOUNT'] }}},0]}}},
{$project: {currency:1,type:1, orderRows: {$map: {input: '$orderRows', as:'orderRow', in : {$cond: [{ '$eq': [ '$$orderRow.discountName', undefined ] }, ['$$orderRow'], ['$$orderRow',{name:'$$orderRow.discountName.name',amount:{$subtract: [ '$$orderRow.amountInclDiscount', '$$orderRow.amount' ]},vatAmount:{$subtract: [ '$$orderRow.vatAmountInclDiscount', '$$orderRow.vatAmount' ]},incomeAccountName:{$concat:['$$orderRow.incomeAccountName',' - Rabatt']},itemType:'$$orderRow.itemType',itemCount:1.0}]]}}}}},
{$unwind : '$orderRows'},
{$project: {currency:1,type:1, orderRows: {$filter: {input: "$orderRows",as: "orderRow",cond: { $ne:['$$orderRow.itemType','DISCOUNT'] }}} }},
{$unwind : '$orderRows'},
{$group : {_id: {incomeAccountName:'$orderRows.incomeAccountName',currency:'$currency',articleName:'$orderRows.name',type:'$type'},itemType:{$max:'$orderRows.itemType'},amount:{$sum:'$orderRows.amount'},taxAmount:{$sum:'$orderRows.vatAmount'},quantity:{$sum:'$orderRows.itemCount'}}},
{$group : {_id: {incomeAccountName:'$_id.incomeAccountName',articleName:'$_id.articleName',type:'$_id.type'},itemType:{$max:'$itemType'},quantity:{$max:'$quantity'},amounts:{$push:{amount:'$amount',currency:'$_id.currency'}},taxAmounts:{$push:{amount:'$taxAmount',currency:'$_id.currency'}}}},
{$group : {_id: {incomeAccountName:'$_id.incomeAccountName',type:'$_id.type'},amounts:{$push:{amount:{$sum:'$amounts.amount'},currency:'$amounts.currency'}},items:{$push:{name:'$_id.articleName',amounts:'$amounts',taxAmounts:'$taxAmounts',quantity:'$quantity',itemType:'$itemType'}}}},
])