如何在MongoDB中执行有条件的算术操作

发布于 2025-02-13 12:44:29 字数 1801 浏览 0 评论 0原文

我遵循模式,

{ 
    "_id" : ObjectId("xxxxx"), 
    "updatedAt" : ISODate("2022-06-29T13:10:36.659+0000"), 
    "createdAt" : ISODate("2022-06-29T08:06:51.264+0000"), 
    "payments" : [
        {
            "paymentId" : "xxxxx", 
            "paymentType" : "charge", 
            "paymentCurrency" : "PKR", 
            "paymentMode" : "cash", 
            "paymentTotal" : 13501.88, 
            "penalties" : 100
        }, 
        {
            "paymentId" : "ccccc", 
            "paymentType" : "refund", 
            "paymentCurrency" : "PKR", 
            "paymentMode" : "", 
            "paymentTotal" : 13061.879999999997, 
            "penalties" : 430.0
        }
    ]
}

付款方式为“费用”,我想获得所有付款总额

如果 给我语法错误,例如

A syntax error was detected at the runtime. Please consider using a higher shell version or use the syntax supported by your current shell.

xxx

blockquote

db.getCollection("booking").aggregate([
    {
        $match: {
            createdAt : {
            "$gte":ISODate("2022-06-28"),
            "$lte":ISODate("2022-06-30"),
        }
      }
    },
    {$unwind: '$payments'}, 
    {
        "$group":{
            "_id" : "$_id",
            "total" : {
                     $sum: "$payments.paymentTotal"
                }
            },
     },
     {
        $project :
        {
            "grandTotal":{
                $cond:{
                    if:{$eq:["$payments.paymentType", "charge"]},
                    then:{$add : {"$total,$payments.paymentTotal"}},
                    else:{ $subtract: {"$total,$payments.paymentTotal"}}
                }
            }
        }
     }
    
]);

,状况和切换语句,但两者都无法正常工作,或者我使用的是错误的。

I've following schema

{ 
    "_id" : ObjectId("xxxxx"), 
    "updatedAt" : ISODate("2022-06-29T13:10:36.659+0000"), 
    "createdAt" : ISODate("2022-06-29T08:06:51.264+0000"), 
    "payments" : [
        {
            "paymentId" : "xxxxx", 
            "paymentType" : "charge", 
            "paymentCurrency" : "PKR", 
            "paymentMode" : "cash", 
            "paymentTotal" : 13501.88, 
            "penalties" : 100
        }, 
        {
            "paymentId" : "ccccc", 
            "paymentType" : "refund", 
            "paymentCurrency" : "PKR", 
            "paymentMode" : "", 
            "paymentTotal" : 13061.879999999997, 
            "penalties" : 430.0
        }
    ]
}

I want to get all paymentTotal sum if paymentType is 'charge' else subtract the paymentTotal from the sum if paymentType is other than charge, i.e refund also subtract penalties from total sum

I've tried following query which is not working giving me syntax error like,

A syntax error was detected at the runtime. Please consider using a higher shell version or use the syntax supported by your current shell.

xxx

Blockquote

db.getCollection("booking").aggregate([
    {
        $match: {
            createdAt : {
            "$gte":ISODate("2022-06-28"),
            "$lte":ISODate("2022-06-30"),
        }
      }
    },
    {$unwind: '$payments'}, 
    {
        "$group":{
            "_id" : "$_id",
            "total" : {
                     $sum: "$payments.paymentTotal"
                }
            },
     },
     {
        $project :
        {
            "grandTotal":{
                $cond:{
                    if:{$eq:["$payments.paymentType", "charge"]},
                    then:{$add : {"$total,$payments.paymentTotal"}},
                    else:{ $subtract: {"$total,$payments.paymentTotal"}}
                }
            }
        }
     }
    
]);

I've tried, Condition and Switch statements but both are not working, or maybe I'm using them wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

つ低調成傷 2025-02-20 12:44:29

您可以使用$ redion为此:

db.collection.aggregate([
  {
    $match: {
      createdAt: {
        $gte: ISODate("2022-06-28T00:00:00.000Z"),
        $lte: ISODate("2022-06-30T00:00:00.000Z")
      }
    }
  },
  {
    $project: {
      grandTotal: {
        $reduce: {
          input: "$payments",
          initialValue: 0,
          in: {
            $cond: [
              {$eq: ["$this.paymentType", "charge"]},
              {$add: ["$this.paymentTotal", "$value"]},
              {$subtract: ["$value", "$this.paymentTotal"]}
            ]
          }
        }
      }
    }
  }
])

查看其在

You can use $reduce for it:

db.collection.aggregate([
  {
    $match: {
      createdAt: {
        $gte: ISODate("2022-06-28T00:00:00.000Z"),
        $lte: ISODate("2022-06-30T00:00:00.000Z")
      }
    }
  },
  {
    $project: {
      grandTotal: {
        $reduce: {
          input: "$payments",
          initialValue: 0,
          in: {
            $cond: [
              {$eq: ["$this.paymentType", "charge"]},
              {$add: ["$this.paymentTotal", "$value"]},
              {$subtract: ["$value", "$this.paymentTotal"]}
            ]
          }
        }
      }
    }
  }
])

See how it works on the playground example

私藏温柔 2025-02-20 12:44:29

您可以做简单的数学:

db.collection.aggregate([
   {
      $set: {
         grandTotal: {
            $map: {
               input: "$payments",
               in: {
                  $multiply: [
                     "$this.paymentTotal",
                     { $cond: [{ $eq: ["$this.paymentType", "charge"] }, 1, -1] }
                  ]
               }
            }
         }
      }
   },
   { $set: { grandTotal: { $sum: "$grandTotal" } } }
])

You can do simple math:

db.collection.aggregate([
   {
      $set: {
         grandTotal: {
            $map: {
               input: "$payments",
               in: {
                  $multiply: [
                     "$this.paymentTotal",
                     { $cond: [{ $eq: ["$this.paymentType", "charge"] }, 1, -1] }
                  ]
               }
            }
         }
      }
   },
   { $set: { grandTotal: { $sum: "$grandTotal" } } }
])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文