繁体   English   中英

如何在mongoDB中使用aggregate获得此结果

[英]How to get this result with aggregate in mongoDB

我有发票收藏......

{
"_id" : 1,
"items" : [ 
    {
        "_id" : 1,
        "value" : 100,
        "price" : 9500
    }, 
    {
        "_id" : 2,
        "value" : 200,
        "price" : 9500
    }
],
"costs" : [ 
    {
        "_id" : 1,
        "price" : 100
    }, 
    {
        "_id" : 2,
        "price" : 150
    }, 
    {
        "_id" : 3,
        "price" : 250
    }
]}

我收到汇总的发票金额......

换句话说=>

{$ sum:{$ multiply:{[$ items.value,$ items.price]}}} - {$ sum:“$ costs.price”};

{
  "_id" : 1,
  "amount" : 2849500
}

非常想......

;-)

Mongo 3.4版

$ map将项目的价格和价值相乘,$ reduce计算项目价格和价值的总和,以及$ reduce来计算成本价格的总和。 $减去早期减少的值以获得最终金额。

aggregate([{
    $project: {
        _id: 1,
        amount: {
            $subtract: [{
                $reduce: {
                    input: {
                        $map: {
                            input: "$items",
                            as: "item",
                            in: {
                                $multiply: ["$$item.value", "$$item.price"]
                            }
                        }
                    },
                    initialValue: 0,
                    in: {
                        $add: ["$$value", "$$this"]
                    }
                }
            }, {
                $reduce: {
                    input: "$costs.price",
                    initialValue: 0,
                    in: {
                        $add: ["$$value", "$$this"]
                    }
                }
            }]
        }

    }
}])

Mongo 3.x版本

第一个$项目乘以项目的价值和价格。 接下来分组以计算项目和成本字段的总和,这将导致每个项目和成本字段和最终项目的一个数组值仅查看来自两个数组的数组值和$ arrayElemAt以相互减去值。

aggregate(
    [{
        $project: {
            vpItems: {
                $map: {
                    input: "$items",
                    as: "item",
                    in: {
                        $multiply: ["$$item.value", "$$item.price"]
                    }
                }
            },
            costs: '$costs'
        }
    }, {
        $group: {
            _id: '$_id',
            vpItems: {
                $addToSet: {
                    $sum: '$vpItems'
                }
            },
            pCosts: {
                $addToSet: {
                    $sum: '$costs.price'
                }
            }
        }
    }, {
        $project: {
            _id: 1,
            amount: {
                $subtract: [{
                    $arrayElemAt: ["$vpItems", 0]
                }, {
                    $arrayElemAt: ["$pCosts", 0]
                }]
            }
        }
    }])

Mongo 2.6版本

$展开项目和组来计算从项目的价格和价值和$展开成本相乘返回的值的总和,以计算项目的价格和价值和项目的总和,以减去先前分组的值以计算最终金额。

aggregate([{
      $unwind: '$items'
  }, {
      $group: {
          _id: '$_id',
          totalItems: {
              $sum: {
                  $multiply: ["$items.value", "$items.price"]
              }
          },
          costs: {
              $first: '$costs'
          }
      }
  }, {
      $unwind: '$costs'
  }, {
      $group: {
          _id: '$_id',
          totalItems: {
              $first: '$totalItems'
          },
          totalPrice: {
              $sum: '$costs.price'
          }
      }
  }, {
      $project: {
          _id: 1,
          amount: {
              $subtract: ['$totalItems', '$totalPrice']
          }
      }
  }])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM