簡體   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