我的医生:
db.org.insert({
"id" : 28,
"organisation" : "Mickey Mouse company",
"country" : "US",
"contactpersons" : [{
"title" : "",
"typecontact" : "D",
"mobilenumber" : "757784854",
"firstname" : "Mickey",
"lastname" : "Mouse",
"emailaddress" : "mickey@mouse.com"
},
{
"title" : "",
"typecontact" : "E",
"mobilenumber" : "757784854",
"firstname" : "Donald",
"lastname" : "Duck",
"emailaddress" : "donald@duck.com"
}],
"modifieddate" : "2013-11-21T16:04:49+0100"
});
我的查询:
mongoexport --host localhost --db sample --collection org --type csv --fields country,contactpersons.0.firstname,contactpersons.0.emailaddress --out D:\info_docs\org.csv
通过此查询,我只能获得联系人的第一个文档值。但是,我也尝试导出第二个文档值。
如何解决此问题?任何人都可以帮我解决这个问题......
答案 0 :(得分:0)
您正在获取contactpersons
中的第一个文档,因为您只导出数组的第一个元素(contactpersons.0.firstname
)。 mongoexport
无法导出数组的多个或所有元素,因此您需要做的是展开数组并将其保存在另一个集合中。您可以使用aggregation framework。
首先,做$unwind
个联系人,然后$project
要使用的字段(在您的示例中为country
和contactpersons
),最后保存输出在$out
的新集合中。
db.org.aggregate([
{$unwind: '$contactpersons'},
{$project: {_id: 0, org_id: '$id', contacts: '$contactpersons', country: 1}},
{$out: 'aggregate_org'}
])
现在您可以执行contacts
的mongoexport($unwind
contactpersons
}和country
的结果。
mongoexport --host localhost --db sample --collection aggregate_org --type=csv --fields country,contacts.firstname,contacts.emailaddress --out D:\info_docs\org.csv