Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays PDI:从MongoDB同时展开两个阵列_Arrays_Mongodb_Pdi - Fatal编程技术网

Arrays PDI:从MongoDB同时展开两个阵列

Arrays PDI:从MongoDB同时展开两个阵列,arrays,mongodb,pdi,Arrays,Mongodb,Pdi,在Spoon中,我使用了mongoDB输入步骤。对于给定的格式文件 {"Number": [ "4700100004" ], "Random": [ "unknown" ], "List_Of_Vals1": [ "3", "2", "1", ], "List_Of_Vals2": [ "1", "2", "3", ]} 我能够使用Mongo query从pdi中展开其中一个阵列 [{"$unwind":"$List_Of_Val

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

{"Number": [
    "4700100004"
],
"Random": [
    "unknown"
],
"List_Of_Vals1": [
    "3",
    "2",
    "1",
],
"List_Of_Vals2": [
    "1",
    "2",
    "3",
]}
我能够使用Mongo query从pdi中展开其中一个阵列

[{"$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"}]
但这将返回“列表1”的副本

我似乎想不出怎样才能做到既不重复也不重复:

Number       Random    List_Of_Vals1    List_Of_Vals2
"4700100004" "unknown" "3"              "1"
"4700100004" "unknown" "2"              "2"
"4700100004" "unknown" "1"              "3"
非常感谢您的帮助。 谢谢

您可以使用Mongo 3.2中引入的
$unwind
选项来实现这一点。对于
$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 } } 
]);
请注意,展开两个大小为
n
的数组将产生
nxn
结果,因此如果两个数组都较大,则可能会遇到限制问题

a的一个即将推出的功能应该能够提供更好的解决方案

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 } } 
]);