使用多级数组打印MongoDB集合中的值

时间:2018-08-22 00:45:29

标签: mongodb mongodb-query

我的要求是将MongoDB集合中具有多级数组的文档弄平,并打印成一行以导出到文本文件。这是示例数据:

db.multiLevelArray.insertMany([{ "field1" : "value 1",
    "field2": [{ field3: "value 3"
                , field4: [{ field5: "value 5"
                            , field6: "value 6"
                            },{field5: "value 55"
                            , field6: "value 66"
                            }]
                , field7: "value 7"
                },
                { field3: "value 23"
                , field4: [{ field5: "value 25"
                            , field6: "value 26"
                            },{field5: "value 255"
                            , field6: "value 266"
                            }]
                , field7: "value 27"
                }
    ]
, field8: "value 8"
},{ "field1" : "value b1",
    "field2": [{ field3: "value b3"
                , field4: [{ field5: "value b5"
                            , field6: "value b6"
                            },{field5: "value b55"
                            , field6: "value b66"
                            }]
                , field7: "value b7"
                },
                { field3: "value b23"
                , field4: [{ field5: "value b25"
                            , field6: "value b26"
                            },{field5: "value b255"
                            , field6: "value b266"
                            }]
                , field7: "value b27"
                }
    ]
, field8: "value b8"
}]);

这是必需的输出:

field1      field3      field5      field6    field7    field8
--------------------------------------------------------------
value 1     value 3     value 5     value 6   value 7   value8
value 1     value 3     value 55    value 56  value 7   value8
value 1     value 3     value 25    value 26  value 7   value8
value 1     value 3     value 255   value 256 value 7   value8

我是MongoDB的新手,非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

首先,您需要使用此查询为数据设置正确的格式,然后使用$out

将输出保存为newMultiLevelArray新集合。
db.multiLevelArray.aggregate([ {"$match" : {"field1" : "value 1"}},
                           {"$unwind" : "$field2"},
                           {"$unwind" : "$field2.field4"},
                           {"$group" : {"_id" : null,
                               "data" : 
                               {"$push" : 
                                    {
                                        "field1" : "$field1",
                                        "field3" : "$field2.field3",
                                        "field5" : "$field2.field4.field5",
                                        "field6" : "$field2.field4.field6",
                                        "field7" : "$field2.field7",
                                        "field8" : "$field8"
                                    }
                                   }
                               }
                           },
                           {"$project" : {"_id" : 0}},
                           {"$unwind" : "$data"},
                           {"$out" : "newMultiLevelArray"}
                           ])

现在,您可以使用mongoexport

导出新集合所需的格式
mongoexport --db db_name --collection new_col_name(newMultiLevelArray) --type csv --fields data.field1,data.field3,data.field6,data.field7,data.field8 --out out_file.csv

样本输出

output-image