$Subtract and other artihmetic operators returning Object instead of int

huangapple go评论63阅读模式
英文:

$Subtract and other artihmetic operators returning Object instead of int

问题

问题

  • 我编写了一些代码,用于执行算术操作,因为我正在开发结算软件。
  • 这段代码片段导致了问题。
    await shopCollection.updateOne(
            { Barcode: elem.Barcode },
            {
              $set: {
                Quantity: { $subtract: ["$Quantity", elem.Quantity] },
                SalesQuantity: {
                  $add: [{ $ifNull: ["$SalesQuantity", 0] }, elem.Quantity],
                },
                Profit: { $subtract: ["$SalesPrice", "$CostPrice"] },
                TotalProfit: {
                  $add: ["$TotalProfit", { $multiply: ["$Profit", elem.Quantity] }],
                },
                SalesTotal: {
                  $add: ["$SalesTotal", { $multiply: ["$SalesPrice", elem.Quantity] }],
                },
              },
            },
            { upsert: true, returnOriginal: false }
          );

完整代码:<https://pastebin.com/b6Zzhv16>

它返回的是对象,而不是整数,如下图所示

$Subtract and other artihmetic operators returning Object instead of int

  • 我正在使用 Ubuntu,运行 MongoDB 社区版,而且我没有使用 Mongoose 或模式。
英文:

Problem

  • I made some code to perform arithmetic operations as I am working on billing software.
  • This code snippet is causing issues
    await shopCollection.updateOne(
            { Barcode: elem.Barcode },
            {
              $set: {
                Quantity: { $subtract: [&quot;$Quantity&quot;, #elem.Quantity] },
                SalesQuantity: {
                  $add: [{ $ifNull: [&quot;$SalesQuantity&quot;, 0] }, elem.Quantity],
                },
                Profit: { $subtract: [&quot;$SalesPrice&quot;, &quot;$CostPrice&quot;] },
                TotalProfit: {
                  $add: [&quot;$TotalProfit&quot;, { $multiply: [&quot;$Profit&quot;, elem.Quantity] }],
                },
                SalesTotal: {
                  $add: [
                    &quot;$SalesTotal&quot;,
                    { $multiply: [&quot;$SalesPrice&quot;, elem.Quantity] },
                  ],
                },
              },
            },
            { upsert: true, returnOriginal: false }
          );

Entire code : <https://pastebin.com/b6Zzhv16>

It returns a object instead of Int like so

$Subtract and other artihmetic operators returning Object instead of int

  • I am using ubuntu ,running the mongodb community edition and I am not using mongoose or schemas

答案1

得分: 2

为了避免这个问题,您将需要使用聚合管道和updateOne操作。除此之外,我发现了一些问题:

  1. 由于您在$set内创建了Profit字段,并且尝试在相同的$set中引用该字段,您将不得不在新的管道$set阶段中执行此操作。
  2. 由于初始数据中没有TotalProfit利润字段,您将不得不在添加之前编写一个ifNull检查,否则最终将得到null
await shopCollection.updateOne(
  {
    Barcode: elem.Barcode
  },
  [
    {
      $set: {
        Quantity: { $subtract: ["$Quantity", elem.Quantity] },
        SalesQuantity: { $add: [{ $ifNull: ["$SalesQuantity", 0] }, elem.Quantity] },
        Profit: { $subtract: ["$SalesPrice", "$CostPrice"] },
        SalesTotal: { $add: ["$SalesTotal", { $multiply: ["$SalesPrice", elem.Quantity] }] }
      }
    },
    {
      $set: {
        TotalProfit: { $add: [{ $ifNull: ["$TotalProfit", 0] }, { $multiply: ["$Profit", elem.Quantity] }] }
      }
    }
  ],
  { upsert: true, returnOriginal: false }
);

playground

英文:

to avoid this you will have to use aggregation pipeline with the updateOne operation.

Apart from that few issues I found

  1. Since you are creating the Profit field inside the $set and you are trying to refer to that field in the same $set you will have to do in a new pipeline $set stage.
  2. Since you don't have TotalProfit profit field in your initial data you will have to write an ifNull check before adding otherwise you end up with null
await shopCollection.updateOne({
  Barcode: elem.Barcode
},
[
  {
    $set: { 
      Quantity: { $subtract: [ &quot;$Quantity&quot;, elem.Quantity ] },
      SalesQuantity: { $add: [ { $ifNull: [ &quot;$SalesQuantity&quot;, 0 ] }, elem.Quantity ] },
      Profit: { $subtract: [ &quot;$SalesPrice&quot;, &quot;$CostPrice&quot; ] },
      SalesTotal: { $add: [ &quot;$SalesTotal&quot;, { $multiply: [ &quot;$SalesPrice&quot;, elem.Quantity ] } ] }
    }
  },
  {
    $set: {
      TotalProfit: { $add: [ { $ifNull: [ &quot;$TotalProfit&quot;, 0 ] }, { $multiply: [ &quot;$Profit&quot;, elem.Quantity ] } ] }
    }
  }
],
  { upsert: true, returnOriginal: false }
);

playground

huangapple
  • 本文由 发表于 2023年5月11日 17:20:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定