Express + Mongoose + 聚合 + 计算可用库存

Posted

技术标签:

【中文标题】Express + Mongoose + 聚合 + 计算可用库存【英文标题】:Express + Mongoose + Aggregation + Calculate Available Stock 【发布时间】:2021-08-05 09:31:48 【问题描述】:

我正在使用 express js 和 mongoose,我是这些平台的新手。如果有人可以帮助我,那就太好了。请参考以下数据,我正在寻找如下输出:

itemsizeId: 609578ca23033e55886e7590, AvailableQuantity: 112

itemsizeId: 609578ca23033e55886e758f, AvailableQuantity: 495

注意:按运动类型(向内/向外)分组。

可用库存:向内 - 向外

[
  
    _id: '609fb1a1a7ed990f30d6cae2',
    refId: 'Purchase-1',
    itemsizeId: '609578ca23033e55886e7590',
    itemId: '609578ca23033e55886e758e',
    sizeId: '60950c0ba4982390f8dfed79',
    movementdate: '2021-05-15T11:33:52.894Z',
    movementtype: 'inward',
    movementcategory: 'purchase',
    quantity: 100,
    id: '609fb1a1a7ed990f30d6cae2',
  ,
  
    _id: '609fb1cba7ed990f30d6cae3',
    refId: 'Purchase-2',
    itemsizeId: '609578ca23033e55886e7590',
    itemId: '609578ca23033e55886e758e',
    sizeId: '60950c0ba4982390f8dfed79',
    movementdate: '2021-05-15T11:34:35.118Z',
    movementtype: 'inward',
    movementcategory: 'purchase',
    quantity: 20,
    id: '609fb1cba7ed990f30d6cae3',
  ,
  
    _id: '609fb1fda7ed990f30d6cae4',
    refId: 'Sale-1',
    itemsizeId: '609578ca23033e55886e7590',
    itemId: '609578ca23033e55886e758e',
    sizeId: '60950c0ba4982390f8dfed79',
    movementdate: '2021-05-15T11:35:25.068Z',
    movementtype: 'outward',
    movementcategory: 'sales',
    quantity: 5,
    id: '609fb1fda7ed990f30d6cae4',
  ,
  
    _id: '609fb255a7ed990f30d6cae5',
    refId: 'Purchase-3',
    itemsizeId: '609578ca23033e55886e758f',
    itemId: '609578ca23033e55886e758e',
    sizeId: '60950be9a4982390f8dfed78',
    movementdate: '2021-05-15T11:36:53.835Z',
    movementtype: 'inward',
    movementcategory: 'purchase',
    quantity: 500,
    id: '609fb255a7ed990f30d6cae5',
  ,
  
    _id: '609fb27ea7ed990f30d6cae6',
    refId: 'Sale-2',
    itemsizeId: '609578ca23033e55886e758f',
    itemId: '609578ca23033e55886e758e',
    sizeId: '60950be9a4982390f8dfed78',
    movementdate: '2021-05-15T11:37:34.066Z',
    movementtype: 'outward',
    movementcategory: 'sales',
    quantity: 8,
    id: '609fb27ea7ed990f30d6cae6',
  ,
]

我已经尝试了下面的代码,我很高兴完成它:

const itemStock = await itemStockMovementModel.aggregate([
  
    $match:  movementtype: 'inward' ,
  ,
  
    $group: 
      _id: 
        itemsizeId: '$itemsizeId',
      ,
      quantity:  $sum: '$quantity' ,
    ,
  ,
])

【问题讨论】:

【参考方案1】: $group by itemsizeId 并按条件获取 inwardoutward 的数量总和 $subtractinward - outward
const itemStock = await itemStockMovementModel.aggregate([
  
    $group: 
      _id: "$itemsizeId",
      inward: 
        $sum:  $cond: [ $eq: ["$movementtype", "inward"] , "$quantity", 0] 
      ,
      outward: 
        $sum:  $cond: [ $eq: ["$movementtype", "outward"] , "$quantity", 0] 
      
    
  ,
  
    $project: 
      quantity: 
        $subtract: ["$inward", "$outward"]
      
    
  
])

Playground


如果您在movementtype 中没有第三个类型,您也可以使用第二个公式,

$groupitemsizeId 检查条件如果movementtypeinward 然后求和quantity 否则求和为负quantity
const itemStock = await itemStockMovementModel.aggregate([
  
    $group: 
      _id: "$itemsizeId",
      quantity: 
        $sum: 
          $cond: [
             $eq: ["$movementtype", "inward"] ,
            "$quantity",
             $subtract: [0, "$quantity"] 
          ]
        
      
    
  
])

Playground

【讨论】:

以上是关于Express + Mongoose + 聚合 + 计算可用库存的主要内容,如果未能解决你的问题,请参考以下文章

Mongoose & Express:从子文档发布数据

Mongoose & Express:从子文档发布数据

如何填充递归模式引用 Mongodb、Mongoose、Express?

使用 Mongoose 和 Express 创建嵌入式文档

Mongoose 虚拟填充和聚合

Nodejs,Mongoose,Express:类型:Schema.ObjectId,参考:'Account' 不起作用