简体   繁体   English

如何将对象数组中的属性从mongoexport导出为CSV?

[英]How do I mongoexport attributes from an array of objects to CSV?

I need to export values of objects in an array to CSV. 我需要将数组中的对象的值导出为CSV。 Let's say my document is: 假设我的文件是:

{
    name:"test",
    types:[
        {type:"A"},
        {type:"B"},
                {type:"C"}
    ]
}

My goal is to produce output like: 我的目标是产生如下输出:

"test", "A"
"test", "B"
"test", "C"

The following would also be acceptable: 以下内容也是可以接受的:

"test", "A,B,C"

I'm trying to accomplish this via mongoexport using: 我正在尝试使用以下方法通过mongoexport实现此目的:

mongoexport -h localhost -d mydb -c mycollection -f name,types.type --csv

Unfortunately, I'm getting: 不幸的是,我得到:

"test",

I've found documentation on referencing specific array elements, like "types.0.type", but the length of my array is unknown. 我找到了有关引用特定数组元素(例如“ types.0.type”)的文档,但是数组的长度未知。 Any ideas? 有任何想法吗?

You'll have to write a custom script that iterates through collections and exports documents in desired format. 您必须编写一个自定义脚本,以遍历集合并以所需格式导出文档。 Built-in mongoexport was not designed for use cases like yours. 内置mongoexport并非为像您这样的用例设计。

If your happy with Perl then the ARJsonLib.pm library in the following article , provides the majority of functionality you'll need, to create your own little toy. 如果您对Perl感到满意,那么下面的文章中的ARJsonLib.pm库提供了创建您自己的小玩具所需的大多数功能。 Note the version in the article is a stub from a toy I hacked together that does exactly what you want along with some other stuff, but as not a mongoDB article it's lacking one function you'll need, that finds the fields/keys in a mongoDB collection, and stores them in an array, but trivial to reconstruct, just write yourself something that pull's n documents from your collection, pushes them into an array and calls findKeysInJsonColl(). 请注意,本文中的版本是我砍在一起的玩具的存根,它确实可以完成您想要的东西以及其他一些东西,但由于不是mongoDB文章,它缺少您需要的一个功能,即可以在其中找到字段/键。 mongoDB集合,并将它们存储在数组中,但是要进行重构很简单,只需编写一些东西,即可从集合中提取n个文档,将它们推入数组并调用findKeysInJsonColl()。 Anyway a couple of the functions will take a MongoDB cursor as parameter, and: 无论如何,有两个函数将MongoDB游标作为参数,并且:

convertToDojoGrid()
convertToExcel()

Again the CSV output is missing, but trivial to add back to convertToExcel(). 再次缺少CSV输出,但是将其添加回convertToExcel()很简单。

eg 例如

...
my $iRows  = convertToExcel("/tmp/test.xlsx", $oMongoData, "", \@aOutFields, "xlsx");
...

Where: $oMongoData is a MongoDB Cursor reference and @aOutFields an array containing the fields/keys you wish to appear in the sheet, 其中:$ oMongoData是MongoDB游标引用,而@aOutFields是包含希望在工作表中显示的字段/键的数组,

You can accomplish what your are trying to do by using the MongoDb's aggregation pipeline operations to build out a temporary collection with the data in the shape you want to export: 您可以通过使用MongoDb的聚合管道操作来构建临时集合,并使用要导出的形状的数据来完成您要尝试的操作:

use mydb
db.mycollection.aggregate([ 
                          {$unwind: "$types"}, 
                          {$project: { _id: 0, name: 1, type: "$types.type" } }, 
                          {$out: "tmp4csv"} 
                          ]);

this will create a tmp4csv collection which contains a document for each element of the array 这将创建一个tmp4csv集合,其中包含该数组每个元素的文档

ie in your example (and adding a _id value as this value has to be taken into consideration in my suggested solution) 即在您的示例中(并添加_id值,因为在我建议的解决方案中必须考虑该值)

{   
    _id: ObjectId("54e3ce75cb87e6d036287cc6"),
    name:"test",
    types:[
        {type:"A"},
        {type:"B"},
        {type:"C"}
        ]
}

through the use of the $unwind operator becomes 通过使用$ unwind运算符成为

{ _id: ObjectId("54e3ce75cb87e6d036287cc6"), name:"test", types:[ {type:"A"} ] },
{ _id: ObjectId("54e3ce75cb87e6d036287cc6"), name:"test", types:[ {type:"B"} ] },
{ _id: ObjectId("54e3ce75cb87e6d036287cc6"), name:"test", types:[ {type:"C"} ] }

Next the $project operator is used to reform the data slightly - unpacking types.type into type and providing a new unique _id value for each document. 接下来,$ project运算符用于稍微修改数据-将type.type解压缩为type,并为每个文档提供一个新的唯一_id值。

Finally the documents generated by the aggregation pipeline are output in to the "tmp4csv" collection using the $out operator. 最终,使用$ out运算符将由聚合管道生成的文档输出到“ tmp4csv”集合中。 Without the creation of a unique _id value in the previous step this step would fail due to duplicate keys. 如果没有在上一步中创建唯一的_id值,则此步骤将由于键重复而失败。

You can then export the data to an output file: 然后可以将数据导出到输出文件:

mongoexport -h localhost -d mydb -c tmp4csv -f name,type --csv --out output.csv

And to keep everything clean I would then drop the tmp4csv collection that was created as part of this: 为了使所有内容保持干净,我将删除在其中创建的tmp4csv集合:

use mydb
db.tmp4csv.drop()

This should give you a csv file with your preferred output format. 这应该会为您提供带有首选输出格式的csv文件。

References: Aggregation pipeline doc: http://docs.mongodb.org/manual/aggregation/ Unwind operator as this is key: http://docs.mongodb.org/manual/reference/operator/aggregation/unwind/ 参考:聚合管道文档: http ://docs.mongodb.org/manual/aggregation/展开运算符,因为这很关键: http : //docs.mongodb.org/manual/reference/operator/aggregation/unwind/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何对对象数组进行分组并去除重复属性? - How do I group array of objects and get rid of repeating attributes? 如何打印存储在数组中的属性而不是对象? - How do I print out the attributes stored in an array instead of the objects? 如何将数组中的对象推送到 csv 中的下一列 - How do i push objects in an array to next column in a csv mongoexport csv输出最后一个数组值 - mongoexport csv output last array values 如何从具有大量属性的对象数组中获取“极简”对象数组 - How to get a "minimalist" array of objects from an array of objects with a pletora of attributes 如何将多个文档中的数组字段合并到MongoDB中的单个输出数组中,并将mongoexport合并到csv中 - How to merge an array field in multiple documents into a single output array in MongoDB and mongoexport to csv 在Rails中,如何确定对象数组是否包含与给定值匹配的特定属性? - In Rails, how do I figure out if an array of objects contains specific attributes matching given values? 如何从对象数组中提取对象(数组)? - How do I extract an object(array) from an array of objects? 如何从另一个数组创建部分对象数组? - How do I create an array of partial objects from another array? 如何将数据从CSV文件添加到Perl中的数组? - How do I add data from a CSV file to an array in Perl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM