英文:
mongodb aggregate subtract by key
问题
我想从'createdAt'值为'A'的pType中减去'createdAt'值为'B'的pType值,并将减去的值相加。
(ex2)
1675408097462(_id:2) - 1675408092026(_id:1)
+
1675408104682(_id:4) - 1675408101259(_id:3)
(ex2)
1675408097462(_id:3) - 1675408095026(_id:2)
+
1675408104682(_id:5) - 1675408101259(_id:4)
我想要使用MongoDB的'aggregate'来获得以下结果:
期望的结果是...
(ex1)
{
"sum_of_diff": "8859"
}
(ex2)
{
"sum_of_diff": "5859"
}
英文:
DB : mongoDB
i have following query result.
(ex1)
[
  {
    "array": [
      {
        "_id": 1,
        "createdAt": NumberLong(1675408092026),
        "pType": "A"
      },
      {
        "_id": 2,
        "createdAt": NumberLong(1675408097462),
        "pType": "B"
      },
      {
        "_id": 3,
        "createdAt": NumberLong(1675408101259),
        "pType": "A"
      },
      {
        "_id": 4,
        "createdAt": NumberLong(1675408104682),
        "pType": "B"
      }
    ]
  }
]
OR
(ex2)
[
  {
    "array": [
      {
        "_id": 1,
        "createdAt": NumberLong(1675408092026),
        "pType": "A"
      },
      {
        "_id": 2,
        "createdAt": NumberLong(1675408095026),
        "pType": "A"
      },
      {
        "_id": 3,
        "createdAt": NumberLong(1675408097462),
        "pType": "B"
      },
      {
        "_id": 4,
        "createdAt": NumberLong(1675408101259),
        "pType": "A"
      },
      {
        "_id": 5,
        "createdAt": NumberLong(1675408104682),
        "pType": "B"
      },
      {
        "_id": 6,
        "createdAt": NumberLong(1675408108682),
        "pType": "B"
      },
      {
        "_id": 7,
        "createdAt": NumberLong(1675408118682),
        "pType": "A"
      }
    ]
  }
]
I want to subtract the 'createdAt' value of pType 'A' from the 'createdAt' value of 'B'<br/>
And I want to add up the subtracted value.<br/>
(ex2)<br/>
1675408097462(_id:2) - 1675408092026(_id:1)<br/>
+<br/>
1675408104682(_id:4) - 1675408101259(_id:3)<br/>
<br/>
(ex2)<br/>
1675408097462(_id:3) - 1675408095026(_id:2)<br/>
+<br/>
1675408104682(_id:5) - 1675408101259(_id:4)<br/>
<br/>
i want to following result using with mongodb 'aggregate' <br/>
please help me.
The expected result is...
(ex1)
{
   "sum_of_diff": "8859"
}
(ex2)
{
   "sum_of_diff": "5859"
}
thank you
答案1
得分: 0
一种选择是使用$reduce与$mergeObjects:
db.collection.aggregate([
  {$project: {
      sum_of_diff: {$reduce: {
          input: "$array",
          initialValue: {lastA: {_id: -1}, sum: 0},
          in: {$mergeObjects: [
              "$$value",
              {$cond: [
                  {$eq: ["$$this.pType", "A"]},
                  {lastA: "$$this"},
                  {$cond: [
                      {$eq: [{$subtract: ["$$this._id", 1]}, "$$value.lastA._id"]},
                      {sum: {
                          $add: [
                            "$$value.sum",
                            {$subtract: ["$$this.createdAt", "$$value.lastA.createdAt"]}
                          ]
                      }},
                      {}
                  ]}
              ]}
          ]}
      }}
  }},
  {$project: {sum_of_diff: "$sum_of_diff.sum", _id: 0}}
])
在playground示例上查看它的工作方式。
英文:
One option is to use $reduce with $mergeObjects:
db.collection.aggregate([
  {$project: {
      sum_of_diff: {$reduce: {
          input: "$array",
          initialValue: {lastA: {_id: -1}, sum: 0},
          in: {$mergeObjects: [
              "$$value",
              {$cond: [
                  {$eq: ["$$this.pType", "A"]},
                  {lastA: "$$this"},
                  {$cond: [
                      {$eq: [{$subtract: ["$$this._id", 1]}, "$$value.lastA._id"]},
                      {sum: {
                          $add: [
                            "$$value.sum",
                            {$subtract: ["$$this.createdAt", "$$value.lastA.createdAt"]}
                          ]
                      }},
                      {}
                  ]}
              ]}
          ]}
      }}
  }},
  {$project: {sum_of_diff: "$sum_of_diff.sum", _id: 0}}
])
See how it works on the playground example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论