我想将csv文件导入到mongo集合中,但是,数据并不完全像我希望它在集合中。
我有以下csv文件:(简化)
city_id, city_name, street_id, street_name
1,city1,1,street1
1,city1,2,street2
1,city1,3,street3
1,city1,4,street4
1,city1,5,street5
2,city2,1,street1
2,city2,2,street2
2,city2,3,street3
2,city2,4,street4
2,city2,5,street5
2,city2,6,street6
2,city2,7,street7
导入后,我希望导入的集合看起来像这样:
{
city_id: 1,
city_name: "city1",
streets: [
{
street_id: 1,
street_name: street1
},
{
street_id: 2,
street_name: street2
},
{
street_id: 3,
street_name: street3
},
{
street_id: 4,
street_name: street4
},
{
street_id: 5,
street_name: street5
}
]
},
{
city_id: 2,
city_name: "city2",
streets: [
{
street_id: 1,
street_name: street1
},
{
street_id: 2,
street_name: street2
},
{
street_id: 3,
street_name: street3
},
{
street_id: 4,
street_name: street4
},
{
street_id: 5,
street_name: street5
},
{
street_id: 6,
street_name: street6
},
{
street_id: 7,
street_name: street7
}
]
}
这可以通过相对简单的方式完成吗?
感谢您的帮助!
答案 0 :(得分:2)
mongoimport.exe /file:city.csv / type:csv / headerline
使用aggreagtion框架组合和重塑文件
db.city.aggregate([
{$group:{
"_id":{"id":"$city_id","city_name":"$city_name" },
"streets": { $push: { street_id: "$street_id", street_name: "$street_name" } }
}},
{$project:{
"_id":0,
"city_id":"$_id.id",
"city_name":"$_id.city_name",
"streets":1
}},
{$out:"cities"}
])
检查城市集合中的搜索结果