MongoDB聚合按键减法

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

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)
{
   &quot;sum_of_diff&quot;: &quot;8859&quot;
}


(ex2)
{
   &quot;sum_of_diff&quot;: &quot;5859&quot;
}

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: &quot;$array&quot;,
          initialValue: {lastA: {_id: -1}, sum: 0},
          in: {$mergeObjects: [
              &quot;$$value&quot;,
              {$cond: [
                  {$eq: [&quot;$$this.pType&quot;, &quot;A&quot;]},
                  {lastA: &quot;$$this&quot;},
                  {$cond: [
                      {$eq: [{$subtract: [&quot;$$this._id&quot;, 1]}, &quot;$$value.lastA._id&quot;]},
                      {sum: {
                          $add: [
                            &quot;$$value.sum&quot;,
                            {$subtract: [&quot;$$this.createdAt&quot;, &quot;$$value.lastA.createdAt&quot;]}
                          ]
                      }},
                      {}
                  ]}
              ]}
          ]}
      }}
  }},
  {$project: {sum_of_diff: &quot;$sum_of_diff.sum&quot;, _id: 0}}
])

See how it works on the playground example

huangapple
  • 本文由 发表于 2023年2月6日 12:07:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357252.html
匿名

发表评论

匿名网友

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

确定