如何使用MongodB聚合将计算值作为字段添加到子文档的现有数组中

时间:2019-07-11 10:14:30

标签: mongodb aggregation-framework

我正在使用MongoDB,并且有一个类似于以下结构的文档。我正在尝试计算总服务成本和每个服务通道成本。这里的服务是子文档。

我的文档结构:

{
  user:'John Papa',active:true,
  proposals:[
   {
      company:'Radical LLC',
      owner:ObjectId(564546...),active:true,
      services:[
         {model:'oneTime',price:3.5, numOfEmp:12, duration:6, dates:''},
         {model:'perMonthly',price:4.5, numOfEmp:8, duration:5, dates:''},
         {model:'perYearly',price:5.5, numOfEmp:6, duration:3, dates:''}
            ]
   },
   {
      company:'Terminal & Co',
      owner:ObjectId(564546...),active:true,
      services:[
          {model:'oneTime',price:6.2, numOfEmp:6, duration:4, dates:''},
          {model:'perMonthly',price:6.5, numOfEmp:2, duration:4, dates:''},
          {model:'perYearly',price:5.2, numOfEmp:3, duration:7, dates:''}
            ]
     }
   ]
  }

像下面的结构一样,期望在聚合后输出:

{
   user:'John Papa'
   proposals:[
          {
            company:'Radical LLC', total_service_cost:321
            owner:ObjectId(564546...),active:true,
            services:[
            {service_cost:42, model:'oneTime',price:3.5, numOfEmp:12, duration:6, dates:''}, // service_cost=(price X numOfEmp)
            {service_cost:180, model:'perMonthly',price:4.5, numOfEmp:8, duration:5, dates:''}, // service_cost=(price X numOfEmp X duration)
            {service_cost:99, model:'perYearly',price:5.5, numOfEmp:6, duration:3, dates:''} // service_cost=(price X numOfEmp X duration)
            ]
          },{
            company:'Terminal & Co', total_service_cost:198.4
            owner:ObjectId(564546...),active:true.
            services:[
            {service_cost:37.2, model:'oneTime',price:6.2, numOfEmp:6, duration:4, dates:''},
            {service_cost:52, model:'perMonthly',price:6.5, numOfEmp:2, duration:4, dates:''},
            {service_cost:109.2, model:'perYearly',price:5.2, numOfEmp:3, duration:7, dates:''}
            ]
          }
          ]
        }

我尝试了以下代码,但输出未按预期显示。

db.users.aggregate([
   { $match: { active: true} },
     {
        $lookup: {
       from: "proposals", let: { ownerId: '$_id', active: true }, 
       pipeline: [
          { $match: { $expr: { $and: [{ $eq: ['$active', '$$active'] }, { $eq: ['$owner', '$$ownerId'] }] } } },
                 { $unwind: '$services' },                            
                        {
                            $addFields: {
                                'services.value': {
                                    $switch: {
                                        branches: [
                                            { case: { $eq: ['$services.model', 'perFte'] }, then: { $multiply: ['$services.numOfEmp', '$services.price'] } },
                                            { case: { $eq: ['$services.model', 'oneTime'] }, then: { $multiply: ['$services.numOfEmp', '$services.price'] } },
                                            { case: { $eq: ['$services.model', 'perMonth'] }, then: { $multiply: ['$services.numOfEmp', '$services.price', '$services.duration'] } },
                                            { case: { $eq: ['$services.model', 'perYear'] }, then: { $multiply: ['$services.numOfEmp', '$services.price', '$services.duration'] } }
                                        ], default: 0
                                    }
                                }
                            }
                        },                                                      
                    ], as: "proposals"
                }
            }
        ])

有什么想法吗?我如何实现以上目标?

0 个答案:

没有答案