按某个字段对集合中的所有键和值进行分组

按某个字段对集合中的所有键和值进行分组

本文介绍了MongoDB 按某个字段对集合中的所有键和值进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的收藏:

id:..
Category:Car
Type:BMW

id:..
Category:Car
Type:Ferrari
Color:Red

id..
Category:Bikes
Type:Mountain Bike

id..
Category:Bikes
Type:BMX

我想进行聚合和分组以获得如下结果:

I want to make an aggregate and a grouping to have a result like this:

[{Category:Car Type:[BMW,Ferrari] Color:[Red]},
{Category:Bikes Type:[Mountain Bike,BMX]}]

所以基本上主要字段是我们的类别,属于该类别的每个文档键都被整理出来,它们的值被分组.

So basically the main field is our Category, and every document keys which is part of that category are sorted out and their values grouped.

是否可以通过聚合来实现这一点?

Is it possible to achieve this with aggregate?

我试图用 $objectToArray 来实现,但它甚至不接近我想要的:

I tried to make it with $objectToArray but it's not even close to what I want:

db.collection.aggregate([
  {
    $project: {
      data: {
        $objectToArray: "$$ROOT"
      }
    }
  },
  {
    $project: {
      data: "$data.k",
      value: "$data.v"
    }
  },
  {
    $unwind: "$data",

  },
  {
    $unwind: "$value",

  },
  {
    $group: {
      _id: null,

      keys: {
        $addToSet: "$data"
      },
      values: {
        $addToSet: "$value"
      }
    }
  }
])

推荐答案

你在正确的轨道上.

将整个对象转换为数组时,还要保存 Category 字段.

When converting the entire object to an array, also save the Category field.

所需的阶段:

  • $project 保存类别并将对象转换为数组
  • $展开数组以分别考虑每个字段
  • $match 删除 _idCategory 和任何其他您不想从数组中分组的字段
  • $group by Categoryk 将每个键的值推送到数组中
  • $group by Category 将键和数组收集在一起
  • $project 将具有收集值的数组转换为对象
  • $addFields 将类别注入到新对象中
  • $replaceRoot 提升新对象
  • $project to save the Category and convert the object to an array
  • $unwind the array to consider each field separately
  • $match to remove _id, Category and any other fields you don't want grouped from the array
  • $group by Category and k to push the values of each key into an array
  • $group by Category to collect the keys and arrays together
  • $project to convert the array with collected values to an object
  • $addFields to inject the Category into the new object
  • $replaceRoot to promote the new object
db.collection.aggregate([
  {$project: {
      Category: 1,
      fields: {$objectToArray: "$$ROOT"}
  }},
  {$unwind: "$fields"},
  {$match: {"fields.k": {$not: {$in: ["_id","Category"]}} }},
  {$group: {
      _id: {
        Category: "$Category",
        key: "$fields.k"
      },
      value: {$push: "$fields.v"}
  }},
  {$group: {
      _id: "$_id.Category",
      fields: {
        $push: {
          "k": "$_id.key",
          "v": "$value"
        }
      }
  }},
  {$project: {
      fields: {$arrayToObject: "$fields"}
  }},
  {$addFields: {
      "fields.Category": "$_id"
  }},
  {$replaceRoot: { newRoot: "$fields"}}
])

游乐场

这篇关于MongoDB 按某个字段对集合中的所有键和值进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:45