计算两个字段的发生次数

时间:2016-06-22 14:56:56

标签: mongodb-aggregation

我在MongoDb集合中有这样的文档:

preg_replace_callback('/&#x([A-Fa-f0-9]{2});/', function ($m) {
    return chr(hexdec($m[1]));
}, $str);

我正在尝试使用MongoDb聚合pipiline计算[ { "_id": { "id": 892, "answer": "C", "level": "regular" }, "total": 4 }, { "_id": { "id": 891, "answer": "Regular", "level": "neutral" }, "total": 3 }, { "_id": { "id": 892, "answer": "B", "level": "regular" }, "total": 3 }, { "_id": { "id": 891, "answer": "Ótimo", "level": "positive" }, "total": 5 }, { "_id": { "id": 892, "answer": "E", "level": "regular" }, "total": 3 }, { "_id": { "id": 891, "answer": "Bom", "level": "positive" }, "total": 1 }, { "_id": { "id": 891, "answer": "Ruim", "level": "negative" }, "total": 2 }, { "_id": { "id": 892, "answer": "D", "level": "regular" }, "total": 3 }, { "_id": { "id": 891, "answer": "Péssimo", "level": "negative" }, "total": 3 }, { "_id": { "id": 892, "answer": "F", "level": "regular" }, "total": 1 } ] answer次。我期待这样的输出:

level

如何使用MongoDb聚合管道实现所需的输出?

编辑:实际上我已经使用$ group来实现类似的东西了,但我想管道中只有一个$ group步骤将无法实现所需的输出。这是我目前的$ group步骤:

[
  {
    "id": 891,
    "answers": [
      {
        "answer": "Ótimo",
        "count": 5
      },
      {
        "answer": "Bom",
        "count": 1
      },
      {
        "answer": "Regular",
        "count": 3
      },
      {
        "answer": "Ruim",
        "count": 2
      },
      {
        "answer": "Péssimo",
        "count": 3
      }
    ],
    "levels": [
      {
        "level": "positive",
        "count": 6
      },
      {
        "level": "neutral",
        "count": 3
      },
      {
        "level": "negative",
        "count": 5
      }
    ],
    "total": 14
  },
  {
    "id": 892,
    "answers": [
      {
        "answer": "B",
        "count": 3
      },
      {
        "answer": "C",
        "count": 4
      },
      {
        "answer": "D",
        "count": 3
      },
      {
        "answer": "E",
        "count": 3
      },
      {
        "answer": "F",
        "count": 1
      },
    ],
    "levels": [
      {
        "level": "regular",
        "count": 14
      }
    ],
    "total": 14
  }
]

这是我到目前为止的输出:

{
  $group: {
    _id: {
      id: "$_id.id"
    },
    answers: {
      $push: {
        answer: "$_id.answer",
        count: "$count"
      }
    },
    levels: {
      $push: {
        level: "$_id.level",
        count: "$count"
      }
    },
    total: { $sum: "$count" }
  }
}

1 个答案:

答案 0 :(得分:0)

以下聚合管道:

{ $group: {
  _id: { id: "$_id.id" },
  answers: {
    $push: {
      answer: "$_id.answer",
      level: "$_id.level",
      count: "$total"
    }
  },
  levels: {
    $push: {
      level: "$_id.level",
      count: "$total"
    }
  }
}},
{ $unwind: $levels },
{ $group: {
  _id: {
    id: "$_id.id",
    level: "$levels.level"
  },
  answers: { $addToSet: "$answers" },
  total: { $sum: "$levels.count" }
}},
{ $group: {
  _id: { id: "$_id.id" },
  answers: { $addToSet: "$answers" },
  levels: {
    $push: {
      level: "$_id.level",
      count: "$total"
    }
  },
  total: { $sum: "$total" }
}},
{ $project: {
  _id: 0,
  id: "$_id.id",
  answers: 1,
  levels: 1,
  total: 1
}}

将产生所需的输出。