Javascript 将多个文档数组展开到新文档中
今天我遇到了一种情况,我需要将mongoDB集合同步到vertica(SQL数据库),其中我的对象键将是SQL中表的列。 我使用mongoDB聚合框架,首先查询、操作和投影想要的结果文档,然后将其同步到vertica 我要聚合的架构如下所示:Javascript 将多个文档数组展开到新文档中,javascript,mongodb,aggregation-framework,Javascript,Mongodb,Aggregation Framework,今天我遇到了一种情况,我需要将mongoDB集合同步到vertica(SQL数据库),其中我的对象键将是SQL中表的列。 我使用mongoDB聚合框架,首先查询、操作和投影想要的结果文档,然后将其同步到vertica 我要聚合的架构如下所示: { userId: 123 firstProperty: { firstArray: ['x','y','z'], anotherAttr: 'abc' }, anotherProperty: { secondArr
{
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'
}
我到底遗漏了什么?我该如何修复它?这实际上是一个比你想象的要“卷曲”得多的问题,它实际上可以归结为“命名键”,这通常是一个真正的问题,你的数据“不应该”在命名这些键时使用“数据点”
您尝试中的另一个明显问题称为“笛卡尔积”。这是一个数组,然后是另一个数组,这会导致“第一个”中的项目对于“第二个”中存在的每个值都重复
要解决第二个问题,基本的方法是“组合阵列”,以便只从单个源执行操作。这在所有剩余的方法中都很常见
至于这些方法,它们在您现有的MongoDB版本和应用程序的一般实用性方面有所不同。因此,让我们逐步了解它们:
删除已命名的密钥
这里最简单的方法就是不要在输出中使用命名键,而是将它们标记为一个“name”
,在最终输出中标识它们的源代码。因此,我们所要做的就是在初始“组合”数组的构造中指定每个“预期”键,然后简单地为由本文档中不存在的命名路径生成的任何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
要实际使用“命名密钥”,我们需要自MongoDB 3.4.4以来才可用的and运算符。使用这些和管道阶段,我们可以简单地处理到所需的输出,而无需在任何阶段明确指定要输出的键:
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" }
]
}
}
}}
])
将“键”转换为数组,然后将“子键”转换为数组,并将这些内部数组中的值映射到输出中的一对键上,这会变得非常可怕
要将“嵌套键”结构“转换”为表示键的“名称”和“值”的“k”
和“v”
数组,基本上需要使用的键部分。它被调用两次,一次用于文档的“外部”部分,并将诸如“\u id”
和“userId”
之类的“常量”字段排除在这样的数组结构中。然后在每个“数组”元素上处理第二个调用,以使这些“内键”成为类似的“数组”
然后使用进行匹配,以确定哪个“内部键”是值的键,哪个是“Attr”。然后这些键在这里被重命名为“外部”键值,我们可以访问该键值,因为这是一个“v”
,由
然后,对于“内部值”即“数组”,我们希望将每个条目合并到一个组合的“数组”中,其基本形式如下:
[
{ "k": "firstProperty", "v": "x" },
{ "k": "firstPropertyAttr", "v": "abc" }
]
对于每个“内部数组”元素,都会发生这种情况,对于每个“内部数组”元素,会反转过程并将每个“k”
和“v”
分别转换为对象的“键”和“值”
由于此时输出仍然是“内部键”的“数组数组”,因此在处理每个元素时,将包装该输出并应用,以便将“数据”
“连接”到单个数组中
剩下的就是简单地从每个源文档生成数组,然后应用,这实际上是允许在每个文档输出的“根”处使用“不同的键名”的部分
这里的“合并”是通过提供一个对象数组来完成的,该数组具有相同的“k”
和“v”
结构,表示为“userId”
,并通过“data”
的转换来实现。当然,这个“新数组”通过最后一次转换为一个对象,它将“object”参数作为表达式形式转换为“newRoot”
当存在大量无法显式命名的“命名键”时,您可以这样做。它实际上给了你想要的结果:
/* 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或更高版本的命名密钥
如果没有上面清单中所示的操作符支持,聚合框架就不可能输出具有不同键名的文档
因此,尽管无法通过$out
指示“服务器”执行此操作,但您当然可以简单地迭代光标并编写一个新集合
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或更多时,最终通过将其写入新集合。当然,这需要与服务器进行“来回”通信,但如果您没有可用于上一次聚合的功能,它确实可以以最有效的方式完成工作
结论
这里的总体要点是,除非您实际上有一个支持它的MongoDB,否则您将无法仅从聚合管道中获得输出中具有“不同键名”的文档
因此,当您没有这种支持时,您可以使用第一个选项,然后使用丢弃命名键。或者您执行最后一种方法,只需操纵光标结果并写回新集合。您的预期结果