使用MongoDB统计从表单数组中选择的每个问题答案的查询

wydwbb8l  于 2022-10-22  发布在  Go
关注(0)|答案(2)|浏览(137)

我对使用MongoDB查询还很陌生。
我想数一数根据问题选出的答案的数量。
以下是我正在尝试处理的数据的示例:

{
    "_id": "910ef391-bc06-4fda-80c5-c6a6d9bcXXXX",
    "book": {
      "register": [
        {
          "questions": [
            {
              "id": "87b8c60c-9c9e-41d1-a52a-588400deXXXX",
              "name": "Question A",
              "answers": [
                {
                  "_id": 1,
                  "label": "Answer 1",
                  "selected": true
                },
                {
                  "_id": 2,
                  "label": "Answer 2",
                  "selected": false
                }
              ]
            },
            {
              "id": "55b8c60c-9c9e-41d1-a52a-588400deXXXX",
              "name": "Question B",
              "answers": [
                {
                  "_id": 1,
                  "label": "Answer 1",
                  "selected": true
                },
                {
                  "_id": 2,
                  "label": "Answer 2",
                  "selected": false
                }
              ]
            }
          ]
        }
      ]
    }
  },
  {
    "_id": "120ef391-bc06-4fda-80c5-c6a6d9bcXXXX",
    "book": {
      "register": [
        {
          "questions": [
            {
              "id": "99b8c60c-9c9e-41d1-a52a-588400deXXXX",
              "name": "Question A",
              "answers": [
                {
                  "_id": 1,
                  "label": "Answer 1",
                  "selected": true
                },
                {
                  "_id": 2,
                  "label": "Answer 2",
                  "selected": false
                }
              ]
            }
          ]
        }
      ]
    }
  }
]

以下是预期结果:

[
  {
    "Answer 1": 2
  }
]

我想数一数问题A的精选答案的数目。
我尝试了一些方法,但没有返回document found

db.collection.aggregate([
  {
    $group: {
      _id: "$book.register.questions",
      questions: {
        $push: "$book.register.questions"
      }
    }
  },
  {
    $match: {
      "questions.name": "Question A"
    }
  }
])

谢谢你的帮助!🤙

fykwrbwg

fykwrbwg1#

查询有点长。
1.$match--用点符号过滤文档,找到包含“问题A”的文档。
$unwind-将book.register数组解构为多个文档。
$unwind-将book.register.questions数组解构为多个文档。
$unwind-将book.register.questions.answers数组解构为多个文档。
1.$group-按book.register.questions.answers.label分组,并根据book.register.questions.answers.selected的值进行条件求和。如果为真,则加1。
1.$group-按null分组。这样做的目的是通过压入答案标签并将其计入answers数组,将所有文档合并为一个文档。
1.$replaceWith-替换输入文档,将answers数组转换为键值对。

db.collection.aggregate([
  {
    $match: {
      "book.register.questions.name": "Question A"
    }
  },
  {
    $unwind: "$book.register"
  },
  {
    $unwind: "$book.register.questions"
  },
  {
    $unwind: "$book.register.questions.answers"
  },
  {
    $group: {
      _id: "$book.register.questions.answers.label",
      count: {
        $sum: {
          $cond: {
            if: {
              $eq: [
                "$book.register.questions.answers.selected",
                true
              ]
            },
            then: 1,
            else: 0
          }
        }
      }
    }
  },
  {
    $group: {
      _id: null,
      answers: {
        $push: {
          k: "$_id",
          v: "$count"
        }
      }
    }
  },
  {
    $replaceWith: {
      $arrayToObject: "$answers"
    }
  }
])

Demo @ Mongo Playground

zdwk9cvp

zdwk9cvp2#

它成功了!只需将第一个$match管道移到$unwind管道之后:"$book.register.questions"。@永顺=>非常感谢你的回答、你的React能力和你的回应质量!

db.collection.aggregate([
  {
    $unwind: "$book.register"
  },
  {
    $unwind: "$book.register.questions"
  },
  {
    $match: {
      "book.register.questions.name": "Question A"
    }
  },
  {
    $unwind: "$book.register.questions.answers"
  },
  {
    $group: {
      _id: "$book.register.questions.answers.label",
      count: {
        $sum: {
          $cond: {
            if: {
              $eq: [
                "$book.register.questions.answers.selected",
                true
              ]
            },
            then: 1,
            else: 0
          }
        }
      }
    }
  },
  {
    $group: {
      _id: null,
      answers: {
        $push: {
          k: "$_id",
          v: "$count"
        }
      }
    }
  },
  {
    $replaceWith: {
      $arrayToObject: "$answers"
    }
  }
])

相关问题