如何在MongoDB中获取对象数组中值的总和。

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

How to get sum of values in array of objects in mongodb

问题

从具有金额和金额日期的对象数组中使用某些操作获取当天的总支出。
英文:

i have a mongodb recor which looks like this .

[
  {
    "_id": {
      "$oid": "64a5a396ec152ab4f5e1c60c"
    },
    "firstName": "Ram",
    "lastName": "Shayam",
    "email": "abc@123.com",
    "transactions": [
      {
        "amount": 22,
        "date": "21/7"
      },
      {
        "amount": 12,
        "date": "21/7"
      },
      {
        "amount": 50,
        "date": "19/7"
      },
      {
        "amount": 100,
        "date": "19/7"
      }
    ]
  }
]

now i gave to filter transactions on the basis of date by doing these operations:

db.collection.aggregate([
  {
    $match: {
      firstName: "Ram"
    }
  },
  {
    $unwind: "$transactions"
  },
  {
    $match: {
      "transactions.date": {
        $eq: "21/7"
      }
    }
  },
  {
    $group: {
      _id: "$_id",
      list: {
        $push: "$transactions"
      }
    }
  }
])

and getting shorted array with transactions of only given dates like this...

[
  {
    "_id": ObjectId("64a5a396ec152ab4f5e1c60c"),
    "list": [
      {
        "amount": 22,
        "date": "21/7"
      },
      {
        "amount": 12,
        "date": "21/7"
      }
    ]
  }
]

**now how to get the total amount spend on that day using some other operations.
like i want to get 34 as output by doing operations.

here is the playground link for more details.**

how to get the total amount spend on that day using some operations from a array of objects with amount and date of amount.

答案1

得分: 4

使用 $sum(aggregation)

db.collection.aggregate([
  {
    $match: {
      firstName: "Ram"
    }
  },
  {
    $unwind: "$transactions"
  },
  {
    $match: {
      "transactions.date": {
        $eq: "21/7"
      }
    }
  },
  {
    $group: {
      _id: "$_id",
      list: {
        $push: "$transactions"
      },
      total: {
        $sum: "$transactions.amount"
      }
    }
  }
])

mongoplayground

英文:

Use $sum(aggregation)

db.collection.aggregate([
  {
    $match: {
      firstName: "Ram"
    }
  },
  {
    $unwind: "$transactions"
  },
  {
    $match: {
      "transactions.date": {
        $eq: "21/7"
      }
    }
  },
  {
    $group: {
      _id: "$_id",
      list: {
        $push: "$transactions"
      },
      total: {
        $sum: "$transactions.amount"
      }
    }
  }
])

mongoplayground

答案2

得分: 1

添加了**$sum**来计算

db.collection.aggregate([
  {
    $match: {
      firstName: "Ram"
    }
  },
  {
    $unwind: "$transactions"
  },
  {
    $match: {
      "transactions.date": {
        $eq: "21/7"
      }
    }
  },
  {
    $group: {
      _id: "$_id",
      list: {
        $push: "$transactions"
      },
      count: {
        $sum: "$transactions.amount"
      }
    }
  }
])
英文:

added $sum to count

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

db.collection.aggregate([
  {
    $match: {
      firstName: &quot;Ram&quot;
    }
  },
  {
    $unwind: &quot;$transactions&quot;
  },
  {
    $match: {
      &quot;transactions.date&quot;: {
        $eq: &quot;21/7&quot;
      }
    }
  },
  {
    $group: {
      _id: &quot;$_id&quot;,
      list: {
        $push: &quot;$transactions&quot;
      },
      count: {
        $sum: &quot;$transactions.amount&quot;
      }
    }
  }
])

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月24日 19:21:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753960.html
匿名

发表评论

匿名网友

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

确定