javascript - 将多个文档数组展开为新文档

标签 javascript mongodb aggregation-framework

今天,我遇到了一种情况,我需要将mongoDB集合同步到vertica(SQL数据库),其中我的对象键将是SQL中表的列。
我使用mongoDB聚合框架,首先查询,操纵和投影所需的结果文档,然后将其同步到vertica。

我要聚合的架构如下所示:

{
  userId: 123
  firstProperty: {
    firstArray: ['x','y','z'],
    anotherAttr: 'abc'
  },
  anotherProperty: {
    secondArray: ['a','b','c'],
    anotherAttr: 'def'
  }  
}


由于数组值与其他数组值不相关,因此我需要的是嵌套数组的每个值都在单独的结果文档中。
为此,我使用以下聚合管道:

db.collection('myCollection').aggregate([
            {
                $match: {
                    $or: [
                        {'firstProperty.firstArray.1': {$exists: true}},
                        {'secondProperty.secondArray.1': {$exists: true}}
                    ]
                }
            },
            {
                $project: {
                    userId: 1,
                    firstProperty: 1,
                    secondProperty: 1
                }
            }, {
                $unwind: {path:'$firstProperty.firstAray'}
            }, {
                $unwind: {path:'$secondProperty.secondArray'},
            }, {
                $project: {
                    userId: 1,
                    firstProperty: '$firstProperty.firstArray',
                    firstPropertyAttr: '$firstProperty.anotherAttr',
                    secondProperty: '$secondProperty.secondArray',
                    seondPropertyAttr: '$secondProperty.anotherAttr'
                }
            }, {
                $out: 'another_collection'
            }
        ])


我期望的结果如下:

{
  userId: 'x1',
  firstProperty: 'x',
  firstPropertyAttr: 'a'
}
{
  userId: 'x1',
  firstProperty: 'y',
  firstPropertyAttr: 'a'
}
{
  userId: 'x1',
  firstProperty: 'z',
  firstPropertyAttr: 'a'
}
{
  userId: 'x1',
  secondProperty: 'a',
  firstPropertyAttr: 'b'
}
{
  userId: 'x1',
  secondProperty: 'b',
  firstPropertyAttr: 'b'
}
{
  userId: 'x1',
  secondProperty: 'c',
  firstPropertyAttr: 'b'
}


相反,我得到这样的东西:

{
  userId: 'x1',
  firstProperty: 'x',
  firstPropertyAttr: 'b'
  secondProperty: 'a',
  secondPropertyAttr: 'b'
}
{
  userId: 'x1',
  firstProperty: 'y',
  firstPropertyAttr: 'b'
  secondProperty: 'b',
  secondPropertyAttr: 'b'
}
{
  userId: 'x1',
  firstProperty: 'z',
  firstPropertyAttr: 'b'
  secondProperty: 'c',
  secondPropertyAttr: 'b'
}


我到底缺少什么,我该如何解决?

最佳答案

实际上,这是一个比您想象的要严重得多的“问题”,而且实际上都归结为“命名键”,这通常是一个实际问题,您的数据“不应”在命名中使用“数据点”这样的键。

您尝试中的另一个明显问题称为“笛卡尔积”。在这里,您$unwind一个数组,然后$unwind另一个数组,这导致“第二”中存在的每个值都重复“第一个” $unwind中的项。

解决第二个问题时,基本方法是“组合数组”,以便仅从单一来源进行$unwind操作。这对于所有其他方法都是很常见的。

至于方法,这些在可用的MongoDB版本和应用程序的一般实用性方面有所不同。因此,让我们逐步解决它们:

删除命名键

这里最简单的方法是根本不希望输出中有命名键,而是将它们标记为"name",以标识最终输出中的源。因此,我们要做的就是在初始“组合”数组的构造中指定每个“期望”键,然后对由本文档中不存在的命名路径产生的任何$filter值简单地null

db.getCollection('myCollection').aggregate([
  { "$match": {
    "$or": [
      { "firstProperty.firstArray.0": { "$exists": true } },
      { "anotherProperty.secondArray.0": { "$exists": true } }
    ]  
  }},
  { "$project": {
    "_id": 0,
    "userId": 1,
    "combined": {
      "$filter": {
        "input": [
          { 
            "name": { "$literal": "first" },
            "array": "$firstProperty.firstArray",
            "attr": "$firstProperty.anotherAttr"
          },
          {
            "name": { "$literal": "another" },
            "array": "$anotherProperty.secondArray",
            "attr": "$anotherProperty.anotherAttr"
          }
        ],
        "cond": {
          "$ne": ["$$this.array", null ]
        }
      }
    }
  }},
  { "$unwind": "$combined" },
  { "$unwind": "$combined.array" },
  { "$project": {
    "userId": 1,
    "name": "$combined.name",
    "value": "$combined.array",
    "attr": "$combined.attr"
  }}
])


根据您的问题中包含的数据,这将产生:

/* 1 */
{
    "userId" : 123.0,
    "name" : "first",
    "value" : "x",
    "attr" : "abc"
}

/* 2 */
{
    "userId" : 123.0,
    "name" : "first",
    "value" : "y",
    "attr" : "abc"
}

/* 3 */
{
    "userId" : 123.0,
    "name" : "first",
    "value" : "z",
    "attr" : "abc"
}

/* 4 */
{
    "userId" : 123.0,
    "name" : "another",
    "value" : "a",
    "attr" : "def"
}

/* 5 */
{
    "userId" : 123.0,
    "name" : "another",
    "value" : "b",
    "attr" : "def"
}

/* 6 */
{
    "userId" : 123.0,
    "name" : "another",
    "value" : "c",
    "attr" : "def"
}


合并对象-最低要求MongoDB 3.4.4

要实际使用“命名键”,我们需要$objectToArray$arrayToObject运算符,这些运算符仅在MongoDB 3.4.4之后可用。使用这些和$replaceRoot管道阶段,我们可以简单地处理您想要的输出,而无需在任何阶段显式命名要输出的键:

db.getCollection('myCollection').aggregate([
  { "$match": {
    "$or": [
      { "firstProperty.firstArray.0": { "$exists": true } },
      { "anotherProperty.secondArray.0": { "$exists": true } }
    ]  
  }},
  { "$project": {
    "_id": 0,
    "userId": 1,
    "data": {
      "$reduce": {
        "input": {
          "$map": {
            "input": {
              "$filter": {
                "input": { "$objectToArray": "$$ROOT" },
                "cond": { "$not": { "$in": [ "$$this.k", ["_id", "userId"] ] } }
              }
            },
            "as": "d",
            "in": {
              "$let": {
                "vars": {
                  "inner": {
                    "$map": {
                      "input": { "$objectToArray": "$$d.v" },
                      "as": "i",
                      "in": {
                        "k": {
                          "$cond": {
                            "if": { "$ne": [{ "$indexOfCP": ["$$i.k", "Array"] }, -1] },
                            "then": "$$d.k",
                            "else": { "$concat": ["$$d.k", "Attr"] }
                          }  
                        },
                        "v": "$$i.v"
                      }
                    }
                  }
                },
                "in": {
                  "$map": {
                    "input": { 
                      "$arrayElemAt": [
                        "$$inner.v",
                        { "$indexOfArray": ["$$inner.k", "$$d.k"] } 
                      ]
                    },
                    "as": "v",
                    "in": {
                      "$arrayToObject": [[
                        { "k": "$$d.k", "v": "$$v" },
                        { 
                          "k": { "$concat": ["$$d.k", "Attr"] },
                          "v": {
                            "$arrayElemAt": [
                              "$$inner.v",
                              { "$indexOfArray": ["$$inner.k", { "$concat": ["$$d.k", "Attr"] }] }
                            ]
                          }
                        }
                      ]]
                    }
                  }
                }
              }
            }
          }
        },
        "initialValue": [],
        "in": { "$concatArrays": [ "$$value", "$$this" ] }
      }
    }
  }},
  { "$unwind": "$data" },
  { "$replaceRoot": {
    "newRoot": {
      "$arrayToObject": {
        "$concatArrays": [
          [{ "k": "userId", "v": "$userId" }],
          { "$objectToArray": "$data" }
        ]
      } 
    }   
  }}
])


通过将“键”转换为数组,然后将“子键”转换为数组,并将这些内部数组的值映射到输出中的一对键,这变得非常可怕。

要使“嵌套键”结构“转换”为表示键“名称”和“值”的$objectToArray"k"数组,本质上需要键部分为"v"。这被调用了两次,一次是针对文档的“外部”部分,另一种是将“恒定”字段(例如"_id""userId")排除在这种数组结构之外。然后,对那些“数组”元素中的每一个进行第二次调用,以使这些“内部键”成为类似的“数组”。

然后使用$indexOfCP进行匹配,以找出哪个“内键”是该值的一个,哪个是“ Attr”。然后,将这些键在此处重命名为“外部”键值,因为这是"v"$objectToArray,所以我们可以访问该值。

然后对于“内部值”(即“数组”),我们想将每个条目$map组合成一个组合的“数组”,其基本形式为:

[
  { "k": "firstProperty", "v": "x" },
  { "k": "firstPropertyAttr", "v": "abc" }
]


每个“内部数组”元素都会发生这种情况,为此$arrayToObject取消该过程,并将每个"k""v"分别转换为对象的“键”和“值”。

由于此时输出仍然是“内部键”的“数组数组”,因此$reduce包装该输出并在处理每个元素时应用$concatArrays以便将“ cc”“合并”为单个数组>。

剩下的只是简单地"data"从每个源文档生成的数组,然后应用$unwind,这实际上是在每个文档输出的“根”处允许“不同的键名”的部分。

这里的“合并”是通过提供标记为$replaceRoot的具有相同"k""v"构造的对象数组,并将其与"userId"$objectToArray转换“结合”来完成的。当然,此“新数组”然后最后一次通过"data"转换为对象,这形成了$arrayToObject的“ object”自变量作为表达式。

当存在大量无法真正明确命名的“命名键”时,您可以执行类似的操作。它实际上为您提供了想要的结果:

/* 1 */
{
    "userId" : 123.0,
    "firstProperty" : "x",
    "firstPropertyAttr" : "abc"
}

/* 2 */
{
    "userId" : 123.0,
    "firstProperty" : "y",
    "firstPropertyAttr" : "abc"
}

/* 3 */
{
    "userId" : 123.0,
    "firstProperty" : "z",
    "firstPropertyAttr" : "abc"
}

/* 4 */
{
    "userId" : 123.0,
    "anotherProperty" : "a",
    "anotherPropertyAttr" : "def"
}

/* 5 */
{
    "userId" : 123.0,
    "anotherProperty" : "b",
    "anotherPropertyAttr" : "def"
}

/* 6 */
{
    "userId" : 123.0,
    "anotherProperty" : "c",
    "anotherPropertyAttr" : "def"
}


没有MongoDB 3.4.4或更高版本的命名键

没有上面清单中所示的操作员支持,聚合框架根本不可能输出具有不同键名的文档。

因此,尽管不可能通过"newRoot"指示“服务器”执行此操作,但是您当然可以简单地迭代游标并编写新的集合

var ops = [];

db.getCollection('myCollection').find().forEach( d => {
  ops = ops.concat(Object.keys(d).filter(k => ['_id','userId'].indexOf(k) === -1 )
    .map(k => 
      d[k][Object.keys(d[k]).find(ki => /Array$/.test(ki))]
        .map(v => ({
          [k]: v,
          [`${k}Attr`]: d[k][Object.keys(d[k]).find(ki => /Attr$/.test(ki))]
      }))
    )
    .reduce((acc,curr) => acc.concat(curr),[])
    .map( o => Object.assign({ userId: d.userId },o) )
  );

  if (ops.length >= 1000) {
    db.getCollection("another_collection").insertMany(ops);
    ops = [];
  }

})

if ( ops.length > 0 ) {
  db.getCollection("another_collection").insertMany(ops);
  ops = [];
}


与早期聚合中所做的事情相同,但只是“外部”。本质上,它为与“内部”数组匹配的每个文档生成文档数组,如下所示:

[ 
    {
        "userId" : 123.0,
        "firstProperty" : "x",
        "firstPropertyAttr" : "abc"
    }, 
    {
        "userId" : 123.0,
        "firstProperty" : "y",
        "firstPropertyAttr" : "abc"
    }, 
    {
        "userId" : 123.0,
        "firstProperty" : "z",
        "firstPropertyAttr" : "abc"
    }, 
    {
        "userId" : 123.0,
        "anotherProperty" : "a",
        "anotherPropertyAttr" : "def"
    }, 
    {
        "userId" : 123.0,
        "anotherProperty" : "b",
        "anotherPropertyAttr" : "def"
    }, 
    {
        "userId" : 123.0,
        "anotherProperty" : "c",
        "anotherPropertyAttr" : "def"
    }
]


它们被“缓存”到一个大数组中,当数组达到1000或更多时,最终将通过$out写入新集合。当然,这需要与服务器进行“来回”通信,但是如果您没有以前的聚合可用的功能,它的确会以最有效的方式完成工作。

结论

这里的总体要点是,除非您实际上有一个支持它的MongoDB,否则您将不会仅从聚合管道中获得带有“不同键名”的文档。

因此,当您没有该支持时,可以选择第一个选项,然后使用.insertMany()丢弃具有命名键的键。或者,您执行最后一种方法,只需操纵游标结果并写回新集合。

关于javascript - 将多个文档数组展开为新文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47024057/

相关文章:

javascript - Jquery/Zend 切换元素禁用不起作用

javascript 将新数组映射到现有数组并添加新属性

javascript - Set State形式函数的返回值

javascript - 这两个将函数分配给 const 的表达式有什么区别?

node.js - 填充多个级别不起作用

mongodb - 如何使用官方 MongoDB Go 驱动程序的 FindOne() 函数从 mongoDB 获取完整文档

MongoDB 按日期以字符串格式聚合不起作用

mongodb - 撤消mongodb中的删除操作

javascript - MongoDB 填充聚合管道分页中的缺失日期

用实际文档替换 ObjectId 数组的 MongoDB 聚合