PDI:从MongoDB同时展开两个数组

时间:2016-03-28 13:24:50

标签: arrays mongodb pdi

在Spoon中我使用了mongoDB输入步骤。对于给定的表格文件..

{"Number": [
    "4700100004"
],
"Random": [
    "unknown"
],
"List_Of_Vals1": [
    "3",
    "2",
    "1",
],
"List_Of_Vals2": [
    "1",
    "2",
    "3",
]}

我可以使用来自pdi

的Mongo查询来展开其中一个数组
[{"$unwind":"$List_Of_Vals1"}]

产生::

Number       Random    List_Of_Vals1    List_Of_Vals2
"4700100004" "unknown" "3"              ["1","2","3"]
"4700100004" "unknown" "2"              ["1","2","3"]
"4700100004" "unknown" "1"              ["1","2","3"]

但最终我需要顺序解开两个数组,我认为我可以通过编写

来完成
[{"$unwind":"$List_Of_Vals1"},{"$unwind":"$List_Of_Vals2"}]

但这会返回" List_Of_Vals1"

的副本
Number       Random    List_Of_Vals1    List_Of_Vals2
"4700100004" "unknown" "3"              "1"
"4700100004" "unknown" "3"              "2"
"4700100004" "unknown" "3"              "3"
...
...
...

我似乎无法弄清楚如何获得这些内容并没有重复,因此:

Number       Random    List_Of_Vals1    List_Of_Vals2
"4700100004" "unknown" "3"              "1"
"4700100004" "unknown" "2"              "2"
"4700100004" "unknown" "1"              "3"

非常感谢任何帮助。 感谢

1 个答案:

答案 0 :(得分:1)

您可以使用Mongo 3.2中引入的$unwind includeArrayIndex选项来实现此目的。对于$unwind操作输出的每个文档,它会添加一个包含数组索引的新字段。

您可以在$project$match阶段使用这些字段将输出过滤到正确的文档子集,即索引匹配的文档。

db.test.aggregate([
  { $unwind: { path: "$List_Of_Vals1", includeArrayIndex : "index1" } },
  { $unwind: { path: "$List_Of_Vals2", includeArrayIndex : "index2" } },
  { $project: { 
         _id : 1,
         Number: 1,
         Random: 1,
         List_Of_Vals1: 1,
         List_Of_Vals2: 1,
         valid: { $eq: ["$index1", "$index2"] } }
   },
  { $match: { valid: true } } 
]);

请注意,展开2个大小为n的数组会产生n x n结果,因此如果两个数组都很大,您可能会遇到限制问题。

$zip operator即将推出的功能可以提供更好的解决方案。