MongoDB - mongoexport嵌套数组中的所有对象 [英] MongoDB - mongoexport all objects in nested array
问题描述
我使用的是MongoDB版本 2.6.x
。我需要从特定的集合导出文档。
mongoexport
是满足需要的工具。但是,我不知道如何导出嵌套数组下的所有对象。下面是我有的示例文档。
{
_id:1,
field_1 :value1,
field_2:value2,
field_array:[
{sub_field_1:sub_val_1,sub_field_2:sub_val_2},
{sub_field_1:sub_val_1,sub_field_2:sub_val_2},
{sub_field_1:sub_val_1,sub_field_2:sub_val_2}
]
}
下面是 mongoexport
p>
mongoexport -d db_name -c collection_name -q'{field_array.sub_field_1:{$ gte:some_value_1,$ lt: some_value_2}}'-fieldFile fields.txt --csv> data_report.csv
其中, fields.txt
具有以下内容
field_array.sub_field_1
pre>
field_array.sub_field_2
我在csv即空字段中获得如下数据。
field_array.sub_field_1,field_array.sub_field_2
,
但是,如果我指定索引
fields.txt中的值
如下所示field_array.0.sub_field_1
field_array.0.sub_field_2
那么,我得到以下数据
field_array.sub_field_1,field_array.sub_field_2
sub_val_1,sub_val_1
即,只返回field_array中的一个对象,但不是全部。
但是,我需要的是如下field_array.sub_field_1,field_array.sub_field_2
sub_val_1,sub_val_1
sub_val_2,sub_val_2
即field_array中的所有对象。
任何帮助?
解决方案MongoExport b
$ b要导出值为数组对象的属性,然后展开数组,使单个文档存储在新集合中,然后导出该集合。
对于实例
数据库:abc
集合:xyzdb.xyz.aggregate([
{$ unwind:$ field_array},
{$ project:{_id:0,field_id:$ _ id ,Innerfield:$ field_array,field_1:1,field_2:1}},
{$ out:aggregate_xyz}
])
MongoExport语法
mongoexport --host < hostname> --db< Database Name> --collection< collection Name> --csv --fields fieldname1,fieldname2 --out fileName.csv
示例:导出以CSV格式
mongoexport --host localhost --db abc --collection aggregate_xyz --csv --fields field_id,field_1,field_2,Innerfield.sub_field_1,Innerfield.sub_field_2 --out important.csv
示例: 以JSON格式导出
mongoexport --host localhost --db abc --collection aggregate_xyz - 字段field_id,field_1,field_2,Innerfield.sub_field_1,Innerfield.sub_field_2 --out important.json
了解详情请访问
$ unwind
https://docs.mongodb.org/v3.0/reference/operator/aggregation/unwind/
$ out
https://docs.mongodb.org/v3.0/reference/operator/aggregation/out/ < a>
$ project
https://docs.mongodb.org/v3.0/reference/operator/aggregation/project/
I'm using MongoDB version
2.6.x
. And I need to export documents from a specific collection.
mongoexport
is the tool which serves the need. However, I do not know how to export all the objects under a nested array. Below is the sample document I have.{ "_id": 1, "field_1": "value1", "field_2": "value2", "field_array": [ {"sub_field_1": "sub_val_1", "sub_field_2": "sub_val_2"}, {"sub_field_1": "sub_val_1", "sub_field_2": "sub_val_2"}, {"sub_field_1": "sub_val_1", "sub_field_2": "sub_val_2"} ] }
Below is the
mongoexport
commandmongoexport -d db_name -c collection_name -q '{"field_array.sub_field_1": {$gte: "some_value_1", $lt: "some_value_2"}}' -fieldFile fields.txt --csv > data_report.csv
where,
fields.txt
has below contentfield_array.sub_field_1 field_array.sub_field_2
I get data as below in the csv i.e empty fields.
field_array.sub_field_1,field_array.sub_field_2 ,
However, if I specify the index value in
fields.txt
like belowfield_array.0.sub_field_1 field_array.0.sub_field_2
then, I get the below data
field_array.sub_field_1,field_array.sub_field_2 sub_val_1,sub_val_1
i.e, only 1 object in the field_array is returned but not all. But, what I need is as below
field_array.sub_field_1,field_array.sub_field_2 sub_val_1,sub_val_1 sub_val_2,sub_val_2
i.e, all objects in the field_array.
Any help?
解决方案MongoExport
To export the property whose value is array of object then unwind the array to make single document and store in new collection then export that collection.
For Instance
Database : abc collection : xyz
db.xyz.aggregate([ {$unwind: "$field_array"}, {$project: { _id:0,field_id:"$_id",Innerfield: "$field_array", "field_1": 1,"field_2":1}}, {$out: "aggregate_xyz"} ])
MongoExport syntax
mongoexport --host <hostname> --db <Database Name> --collection <collection Name> --csv --fields fieldname1,fieldname2 --out fileName.csv
Example : Export in CSV Format
mongoexport --host localhost --db abc --collection aggregate_xyz --csv --fields field_id,field_1,field_2,Innerfield.sub_field_1,Innerfield.sub_field_2 --out important.csv
Example : Export in JSON Format
mongoexport --host localhost --db abc --collection aggregate_xyz --fields field_id,field_1,field_2,Innerfield.sub_field_1,Innerfield.sub_field_2 --out important.json
To know more please visit
$unwind
https://docs.mongodb.org/v3.0/reference/operator/aggregation/unwind/
$out
https://docs.mongodb.org/v3.0/reference/operator/aggregation/out/
$project
https://docs.mongodb.org/v3.0/reference/operator/aggregation/project/
这篇关于MongoDB - mongoexport嵌套数组中的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!