#mongodb #aggregation-framework
#mongodb #агрегация-фреймворк
Вопрос:
Я пытаюсь обновить значение внутри карты в проекции. Как я могу обновить поле внутри карты? Я пытаюсь выполнить вычисление для chargeAmount, которое будет обновляться в цикле.
Я использую оболочку MongoDB версии v4.0.6 в ubuntu 18.04
{
$project : {
"initialAmount": "$chargeAmount",
"taxedData":{
$map:
{
input: "$rateTaxLevel",
as: "taxIds",
in: {
$map:
{
input: "$taxValidity",
as: "levels",
in: {
//$add: [ "$taxIds.taxid", "$levels.taxid" ]
taxingAmount:
{
$cond: [ { $eq: [ "$taxIds.taxid", "$levels.taxid" ] },
{ $divide: [ "initialAmount",{ $add: [ 1, "$levels.tax_validityval" ]} ]
//"$levels.tax_validityval"
} ,"$REMOVE"
]
},
"chargeAmount" : "$taxingAmount"
}
}
}
}
}
}
}
набор данных, который использовался на стадии проекта
{
"_id" : ObjectId("5caae957beca0f79bddc46d9"),
"uuid" : "urn:uuid:dbc692c7-898c-42c0-ae4d-73f03ed39434:0001PAYMENT93",
"responseTime" : NumberLong(1552589459734),
"direction" : "nb",
"api" : "payment",
"apiID" : "payment_v1",
"applicationName" : "buddhitest",
"applicationId" : NumberInt(93),
"serviceProvider" : "buddhinipun@carbon.super",
"serviceProviderId" : "buddhinipun@carbon.super",
"operatorName" : "OPERATOR1",
"operatorId" : NumberInt(1),
"operation" : "Charge",
"operatorRef" : "ABC-11111113589301",
"chargeAmount" : NumberInt(100),
"category" : "111",
"subcategory" : "222",
"merchant" : "111",
"responseCount" : "1",
"rateCard" : "common_percentage",
"price" : -9.1,
"year" : NumberInt(2019),
"month" : NumberInt(3),
"day" : NumberInt(14),
"hour" : NumberInt(19),
"status" : NumberInt(0),
"statusInfo" : "ok",
"userType" : " Postpaid",
"subRateNB" : [
{
"_id" : ObjectId("5cac45a037c54a0f4ce2b717"),
"sub_rate_nbid" : NumberInt(17),
"api_operationid" : NumberInt(1),
"api_version" : "v1",
"applicationid" : NumberInt(93),
"rate_defid" : NumberInt(30),
"sub_rate_nbactdate" : null,
"sub_rate_nbdisdate" : null,
"createdby" : null,
"createddate" : ISODate("2019-01-16T18:31:18.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2019-01-16T18:31:18.000 0000")
}
],
"rateTaxLevel" : [
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a7"),
"rate_taxesid" : NumberInt(177),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(40),
"level" : NumberInt(1)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a8"),
"rate_taxesid" : NumberInt(178),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(41),
"level" : NumberInt(2)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a9"),
"rate_taxesid" : NumberInt(179),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(1),
"level" : NumberInt(3)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7aa"),
"rate_taxesid" : NumberInt(180),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(66),
"level" : NumberInt(4)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7ab"),
"rate_taxesid" : NumberInt(181),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(68),
"level" : NumberInt(5)
}
],
"taxValidity" : [
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7be"),
"idtax_validityid" : NumberInt(1),
"tax_validityactdate" : ISODate("2018-10-09T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2020-10-09T18:30:00.000 0000"),
"tax_validityval" : 0.02,
"taxid" : NumberInt(1),
"createdby" : null,
"createddate" : ISODate("2018-10-26T13:05:58.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-26T13:05:58.000 0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d5"),
"idtax_validityid" : NumberInt(27),
"tax_validityactdate" : ISODate("2018-10-29T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2035-11-10T18:30:00.000 0000"),
"tax_validityval" : 0.5,
"taxid" : NumberInt(40),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:31:40.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:31:40.000 0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d6"),
"idtax_validityid" : NumberInt(28),
"tax_validityactdate" : ISODate("2018-10-31T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2018-11-07T18:30:00.000 0000"),
"tax_validityval" : 2.0,
"taxid" : NumberInt(41),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:42:20.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:42:20.000 0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ec"),
"idtax_validityid" : NumberInt(50),
"tax_validityactdate" : ISODate("2018-11-11T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2018-11-30T18:30:00.000 0000"),
"tax_validityval" : 0.23,
"taxid" : NumberInt(66),
"createdby" : null,
"createddate" : ISODate("2018-11-12T20:22:47.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-12T20:22:47.000 0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ee"),
"idtax_validityid" : NumberInt(52),
"tax_validityactdate" : ISODate("2018-11-30T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2018-12-07T18:30:00.000 0000"),
"tax_validityval" : 0.01,
"taxid" : NumberInt(68),
"createdby" : null,
"createddate" : ISODate("2018-11-26T15:02:51.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-26T15:02:51.000 0000")
}
]
}
// ----------------------------------------------
{
"_id" : ObjectId("5caae957beca0f79bddc46da"),
"uuid" : "urn:uuid:a566d97b-8032-47d2-aef3-ad5807af3742:0001PAYMENT93",
"responseTime" : NumberLong(1552895806695),
"direction" : "nb",
"api" : "payment",
"apiID" : "payment_v1",
"applicationName" : "buddhitest",
"applicationId" : NumberInt(93),
"serviceProvider" : "buddhinipun@carbon.super",
"serviceProviderId" : "buddhinipun@carbon.super",
"operatorName" : "OPERATOR1",
"operatorId" : NumberInt(1),
"operation" : "Charge",
"operatorRef" : "ABC-123",
"chargeAmount" : NumberInt(10),
"category" : "",
"subcategory" : "",
"merchant" : "",
"responseCount" : "1",
"rateCard" : "common_percentage",
"price" : -0.91,
"year" : NumberInt(2019),
"month" : NumberInt(3),
"day" : NumberInt(18),
"hour" : NumberInt(8),
"status" : NumberInt(0),
"statusInfo" : "ok",
"userType" : " Postpaid",
"subRateNB" : [
{
"_id" : ObjectId("5cac45a037c54a0f4ce2b717"),
"sub_rate_nbid" : NumberInt(17),
"api_operationid" : NumberInt(1),
"api_version" : "v1",
"applicationid" : NumberInt(93),
"rate_defid" : NumberInt(30),
"sub_rate_nbactdate" : null,
"sub_rate_nbdisdate" : null,
"createdby" : null,
"createddate" : ISODate("2019-01-16T18:31:18.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2019-01-16T18:31:18.000 0000")
}
],
"rateTaxLevel" : [
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a7"),
"rate_taxesid" : NumberInt(177),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(40),
"level" : NumberInt(1)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a8"),
"rate_taxesid" : NumberInt(178),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(41),
"level" : NumberInt(2)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7a9"),
"rate_taxesid" : NumberInt(179),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(1),
"level" : NumberInt(3)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7aa"),
"rate_taxesid" : NumberInt(180),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(66),
"level" : NumberInt(4)
},
{
"_id" : ObjectId("5cac45c437c54a0f4ce2b7ab"),
"rate_taxesid" : NumberInt(181),
"rate_defid" : NumberInt(30),
"taxid" : NumberInt(68),
"level" : NumberInt(5)
}
],
"taxValidity" : [
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7be"),
"idtax_validityid" : NumberInt(1),
"tax_validityactdate" : ISODate("2018-10-09T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2020-10-09T18:30:00.000 0000"),
"tax_validityval" : 0.02,
"taxid" : NumberInt(1),
"createdby" : null,
"createddate" : ISODate("2018-10-26T13:05:58.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-26T13:05:58.000 0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d5"),
"idtax_validityid" : NumberInt(27),
"tax_validityactdate" : ISODate("2018-10-29T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2035-11-10T18:30:00.000 0000"),
"tax_validityval" : 0.5,
"taxid" : NumberInt(40),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:31:40.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:31:40.000 0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7d6"),
"idtax_validityid" : NumberInt(28),
"tax_validityactdate" : ISODate("2018-10-31T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2018-11-07T18:30:00.000 0000"),
"tax_validityval" : 2.0,
"taxid" : NumberInt(41),
"createdby" : null,
"createddate" : ISODate("2018-10-30T17:42:20.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-10-30T17:42:20.000 0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ec"),
"idtax_validityid" : NumberInt(50),
"tax_validityactdate" : ISODate("2018-11-11T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2018-11-30T18:30:00.000 0000"),
"tax_validityval" : 0.23,
"taxid" : NumberInt(66),
"createdby" : null,
"createddate" : ISODate("2018-11-12T20:22:47.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-12T20:22:47.000 0000")
},
{
"_id" : ObjectId("5cac469137c54a0f4ce2b7ee"),
"idtax_validityid" : NumberInt(52),
"tax_validityactdate" : ISODate("2018-11-30T18:30:00.000 0000"),
"tax_validitydisdate" : ISODate("2018-12-07T18:30:00.000 0000"),
"tax_validityval" : 0.01,
"taxid" : NumberInt(68),
"createdby" : null,
"createddate" : ISODate("2018-11-26T15:02:51.000 0000"),
"updatedby" : null,
"updateddate" : ISODate("2018-11-26T15:02:51.000 0000")
}
]
}
// ----------------------------------------------
Я получил этот результат, используя функцию Java script
"------------------------------------"
"l1Tax% : 1.02"
"l1Tax : 98.0392156862745"
"l2Tax% : 1.5"
"l2Tax : 65.359477124183"
"l3Tax% : 3"
"l3Tax : 21.78649237472767"
"l4Tax% : 1.23"
"l4Tax : 17.712595426607862"
"l5Tax% : 1.01"
"l5Tax : 17.537223194661248"
"------------------------------------"
"l1Tax% : 1.02"
"l1Tax : 9.803921568627452"
"l2Tax% : 1.5"
"l2Tax : 6.535947712418301"
"l3Tax% : 3"
"l3Tax : 2.1786492374727673"
"l4Tax% : 1.23"
"l4Tax : 1.7712595426607864"
"l5Tax% : 1.01"
"l5Tax : 1.7537223194661251"
используя приведенную ниже функцию Java script, я смог получить желаемый результат, но в подходе Java script наблюдается значительное снижение производительности.
forEach(function (doc) {
var taxableamount = doc.chargeAmount;
var taxedAmmount =0;
printjson("------------------------------------");
for (index = 0; index < doc.rateTaxLevel.length; index ) {
for (index = 0; index < doc.taxValidity.length; index ) {
taxedAmmount = taxableamount / (1 doc.taxValidity[index].tax_validityval);
taxableamount = taxedAmmount;
printjson("l" doc.rateTaxLevel[index].level "Tax% : " (1 doc.taxValidity[index].tax_validityval));
printjson("l" doc.rateTaxLevel[index].level "Tax : " taxableamount);
}
}
}
Комментарии:
1. пожалуйста, предоставьте набор данных и ожидаемый результат
2. @matthPen я обновил вопрос запрошенной информацией