【问题标题】:Make the sum of booleans as integers with MongoDB使用 MongoDB 将布尔值的总和设为整数
【发布时间】:2019-06-13 22:26:28
【问题描述】:

我正在尝试将集合中每个文档的布尔值(其中 true 表示 1 和 false -1)汇总到一个数组中,然后对其进行排序。

我正在使用带有$addFields$sum$cond 的MongoDB 聚合管道。

这里是游乐场:https://play.db-ai.co/m/XQLKqbkkgAABTFVm

管道:

[
  {
    "$addFields": {
      "score": {
        "$sum": {
              "$cond": [
                "$votes.value",
                1,
                -1
              ]
            }
      }
    }
  },
  {
    "$sort": {
      "score": -1
    }
  }
]

收藏:

[
  {
    "votes": [
      {
        "value": true
      },
      {
        "value": true

      },
      {
        "value": true

      },
      {
        "value": false

      }
    ]
  },
  {
    "votes": [
      {
        "value": true

      },
      {
        "value": true

      },
      {
        "value": false

      },
      {
        "value": false

      }
    ]
  }
]

实际结果:

[
  {
    "_id": ObjectId("000000000000000000000000"),
    "votes": [
      {
        "value": true
      },
      {
        "value": true
      },
      {
        "value": true
      },
      {
        "value": false
      }
    ],
    "score": 1
  },
  {
    "_id": ObjectId("000000000000000000000001"),
    "votes": [
      {
        "value": true
      },
      {
        "value": true
      },
      {
        "value": false
      },
      {
        "value": false
      }
    ],
    "score": 1
  }
]

我想要什么:

[{
    "_id": ObjectId("000000000000000000000000"),
    "votes": [
      {
        "value": true
      },
      {
        "value": true
      },
      {
        "value": true
      },
      {
        "value": false
      }
    ],
    "score": 2
}, {
 "_id": ObjectId("000000000000000000000001"),
    "votes": [
      {
        "value": true
      },
      {
        "value": true
      },
      {
        "value": false
      },
      {
        "value": false
      }
    ],
    "score": 0
}]

【问题讨论】:

    标签: arrays mongodb sorting sum boolean


    【解决方案1】:

    我通过展开数组然后再次按 _id 分组来使其工作。

    查看游乐场:https://play.db-ai.co/m/XQMFlZAtYAABLHtL

    [
      {
        "$unwind": {
          "path": "$votes"
        }
      },
      {
        "$group": {
          "_id": "$_id",
          "votes": {
            "$push": "$votes"
          },
          "score": { 
            "$sum": {
              "$cond": [
                "$votes.value",
                1,
                -1
              ]
            } 
          }
        }
      },
      {
        "$sort": {
          "score": -1
        }
      }
    ]
    

    【讨论】:

      【解决方案2】:

      为了解决我的问题,我多次使用$map。 @Plancke 的解决方案正在运行,但之后我在使用 $match 时遇到了问题(它总是没有结果)。

      [
          {
            $addFields: {
              scoresInBoolean: {
                $map: {
                  input: '$votes',
                  as: 'vote',
                  in: '$$vote.value',
                },
              },
            },
          }, {
            $addFields: {
              scoresInInteger: {
                $map: {
                  input: '$scoresInBoolean',
                  as: 'scoreInBoolean',
                  in: {
                    $cond: [
                      '$$scoreInBoolean',
                      1,
                      -1,
                    ],
                  },
                },
              },
            },
          }, {
            $addFields: {
              score: {
                $sum: '$scoresInInteger',
              },
            },
          }
      ]
      

      【讨论】:

        猜你喜欢
        • 2016-05-01
        • 1970-01-01
        • 2017-05-04
        • 2013-12-03
        • 2018-10-22
        • 2022-09-30
        • 1970-01-01
        • 2017-01-26
        • 1970-01-01
        相关资源
        最近更新 更多