Mongodb - 使用聚合框架对多个字段进行分组

标签 mongodb mongodb-query aggregation-framework

我有一些文件

{name: 'apple',        type: 'fruit',  color: 'red'}
{name: 'banana',       type: 'fruit',  color: 'yellow'}
{name: 'orange',       type: 'fruit',  color: 'orange'}
{name: 'eggplant',     type: 'vege',   color: 'purple'}
{name: 'brocoli',      type: 'vege',   color: 'green'}
{name: 'rose',         type: 'flower', color: 'red'}
{name: 'cauli',        type: 'vege',   color: 'white'}
{name: 'potato',       type: 'vege',   color: 'brown'}
{name: 'onion',        type: 'vege',   color: 'white'}
{name: 'strawberry',   type: 'fruit',  color: 'red'}
{name: 'cashew',       type: 'nut',    color: ''}
{name: 'almond',       type: 'nut',    color: ''}
{name: 'lemon',        type: 'vege',   color: 'yellow'}
{name: 'tomato',       type: 'vege',   color: 'red'}
{name: 'tomato',       type: 'fruit',  color: 'red'}
{name: 'fig',          type: 'fruit',  color: 'pink'}
{name: 'nectarin',     type: 'fruit',  color: 'pink'}

我想将它们分组为如下所示的字母

{
 _id:'a',
 name:['apple','almond'],
 type:[],
 color:[]
}

{
 _id:'b',
 name:['banana','brocoli'],
 type:[],
 color:['brown']
}

...

{
 _id:'f',
 name:['fig'],
 type:['fruit','flower'],
 color:['']
}

...

{
 _id:'n',
 name:['nectarin'],
 type:['nut'],
 color:['']
}

...

{
 _id:'p',
 name:['potato'],
 type:[''],
 color:['pink','purple']
}

...

结果可以保存到另一个集合中。因此,我可以在新创建的集合中发出查询:find({_id:'a'}) 返回以字母“a”开头的名称、类型和颜色。

我考虑过使用$group

$group: {
  _id: $substr: ['$name', 0, 1],
  name: {$addToSet: '$name'},
}

然后是另一个命令

$group: {
  _id: $substr: ['$type', 0, 1],
  name: {$addToSet: '$type'},
}

$group: {
  _id: $substr: ['$color', 0, 1],
  name: {$addToSet: '$color'},
}

但我不知道如何将这三者统一在一起以保存到新的集合中。还是聚合框架不适合这种数据汇总?

在现实世界的例子中,例如一个电子商务网站,首页显示类似以下内容:“目前我们有来自 111 品牌的 231 类别下的 135636 产品”。当然,这些数字应该缓存在某个地方(在内存中或在另一个集合中),因为每次运行 $group 都是资源密集型的?这些情况的最佳架构/设计是什么?

抱歉,我的问题有点“令人困惑”。

最佳答案

由于这里有多个数组,因此关键是将它们全部“合并”为一个以进行最简单的处理。

$map聚合框架的运算符在这里运行良好,并且转换元素,以便您从数据中的每个单词中获取“第一个字母”:

db.alpha.aggregate([
  { "$project": {
    "list": {
      "$map": { 
        "input": [ "A", "B", "C" ],
        "as": "el",
        "in": {
          "$cond": [
            { "$eq": [ "$$el", "A" ] },
            { 
              "type": { "$literal": "name" }, 
              "value": "$name",
              "alpha": { "$substr": [ "$name",0,1 ] } 
            },
            { "$cond": [
              { "$eq": [ "$$el", "B" ] },
              {
                "type": { "$literal": "type" }, 
                "value": "$type",
                "alpha": { "$substr": [ "$type",0,1 ] } 
              },
              {
                "type": { "$literal": "color" }, 
                "value": "$color",
                "alpha": { "$substr": [ "$color",0,1 ] } 
              }
            ]}
          ]
        }
      }
    }
  }},
  { "$unwind": "$list" },
  { "$match": { "list.alpha": { "$ne": "" } } },
  { "$group": {
    "_id": "$list.alpha",
    "list": { 
      "$addToSet": "$list"
    }
  }},
  { "$project": {
    "name": { 
      "$setDifference": [
        { "$map": {
          "input": "$list",
          "as": "el",
          "in": {
            "$cond": [
              { "$eq": [ "$$el.type", "name" ] },
              "$$el.value",
              false
            ]
          }
        }},
        [false]
      ]
    },
    "type": { 
      "$setDifference": [
        { "$map": {
          "input": "$list",
          "as": "el",
          "in": {
            "$cond": [
              { "$eq": [ "$$el.type", "type" ] },
              "$$el.value",
              false
            ]
          }
        }},
        [false]
      ]
    },
    "color": { 
      "$setDifference": [
        { "$map": {
          "input": "$list",
          "as": "el",
          "in": {
            "$cond": [
              { "$eq": [ "$$el.type", "color" ] },
              "$$el.value",
              false
            ]
          }
        }},
        [false]
      ]
    }
  }},
  { "$sort": { "_id": 1 } }
])

如果您查看“阶段”中的数据,就会很容易理解转换过程中发生的事情。

第一阶段将所有字段“映射”到每个文档的单个数组中,因此所有文档现在如下所示:

{
    "_id" : ObjectId("55df0652c9064ef625d7f36e"),
    "list" : [
            {
                    "type" : "name",
                    "value" : "nectarin",
                    "alpha" : "n"
            },
            {
                    "type" : "type",
                    "value" : "fruit",
                    "alpha" : "f"
            },
            {
                    "type" : "color",
                    "value" : "pink",
                    "alpha" : "p"
            }
    ]
}

$unwind影响不大,因为它遵循标准并为每个成员创建新文档。这是$group它完成了这里的大部分工作,并在分组中按照“alpha”得到了这个结果:

{
    "_id" : "o",
    "list" : [
            {
                    "type" : "name",
                    "value" : "orange",
                    "alpha" : "o"
            },
            {
                    "type" : "color",
                    "value" : "orange",
                    "alpha" : "o"
            },
            {
                    "type" : "name",
                    "value" : "onion",
                    "alpha" : "o"
            }
    ]
}

它有一个很好的分组,并且可以说是一个不错的输出格式。但为了获得最终结果,需要再次使用 $map 运算符和 $setDifference 一起使用。它可用于删除每个字段“类型”转换与所需输出字段不匹配的 false 值。

完整结果是:

{ "_id" : "a", "name" : [ "almond", "apple" ], "type" : [ ], "color" : [ ] }
{ "_id" : "b", "name" : [ "brocoli", "banana" ], "type" : [ ], "color" : [ "brown" ] }
{ "_id" : "c", "name" : [ "cashew", "cauli" ], "type" : [ ], "color" : [ ] }
{ "_id" : "e", "name" : [ "eggplant" ], "type" : [ ], "color" : [ ] }
{ "_id" : "f", "name" : [ "fig" ], "type" : [ "flower", "fruit" ], "color" : [ ] }
{ "_id" : "g", "name" : [ ], "type" : [ ], "color" : [ "green" ] }
{ "_id" : "l", "name" : [ "lemon" ], "type" : [ ], "color" : [ ] }
{ "_id" : "n", "name" : [ "nectarin" ], "type" : [ "nut" ], "color" : [ ] }
{ "_id" : "o", "name" : [ "onion", "orange" ], "type" : [ ], "color" : [ "orange" ] }
{ "_id" : "p", "name" : [ "potato" ], "type" : [ ], "color" : [ "pink", "purple" ] }
{ "_id" : "r", "name" : [ "rose" ], "type" : [ ], "color" : [ "red" ] }
{ "_id" : "s", "name" : [ "strawberry" ], "type" : [ ], "color" : [ ] }
{ "_id" : "t", "name" : [ "tomato" ], "type" : [ ], "color" : [ ] }
{ "_id" : "v", "name" : [ ], "type" : [ "vege" ], "color" : [ ] }
{ "_id" : "w", "name" : [ ], "type" : [ ], "color" : [ "white" ] }
{ "_id" : "y", "name" : [ ], "type" : [ ], "color" : [ "yellow" ] }

所有内容都按字母顺序分组,每个字段都有自己的数组。

即将发布的 MongoDB 将有一个 $filter ,它使 $map$setDifference 组合变得更好一些。但这并不构成“集合”,只要$addToSet对这个过程来说并不重要。就地就业。


考虑到这一点,我想“建议”考虑到您要在此处处理的数据量,每个字母的结果“数组”可能会超过 BSON 限制,具体取决于有多少个不同的“单词”实际上是。

在这种情况下,这里的“建议”将遵循一直到并包括 $match 的流程,但之后只有 $group ,如下所示:

  { "$group": {
    "_id": { 
      "alpha": "$list.alpha",
      "type": "$list.type",
      "value": "$list.value",
    }
  }},
  { "$sort": { "_id": 1 } }

当然,它的输出更长,但在任何阶段都不会超过文档的 BSON 限制。

关于Mongodb - 使用聚合框架对多个字段进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32248903/

相关文章:

php - Laravel 5 + MongoDB

mongodb - 如何使用 Perl 对 mongodb 中的多个字段进行排序?

javascript - 在 MongoDB 3.0 中的聚合查询中获取数组的前 2 个元素

mongodb - 如何将具有静态值的字段添加到 mongodb 查找查询?

node.js - MongoDB Collection 聚合多个对象的总和

node.js - 如何在 mongodb 中的嵌入文档字段上使用 $lookup

javascript - MongoDB:如何获取特定文档的计数?

mongodb - 带有副本集的 Mongo 容器,在 docker-compose 中只有一个节点

javascript - 如何将 Mongoose 中的鉴别器连接到我的父文档?尝试导入数据时出错

mongodb - 在 $sort 聚合管道之后向文档添加字段,该管道使用 MongoDb 聚合将其索引包含在排序列表中