我想从CSV
数据库进行特定的mongo
导出。
以下是我的两份文件:
{'name': 'square fonction', 'data':[{'x':1,'y':1}, {'x':2,'y':4}, {'x':3, 'y':9}]}
{'name': 'incremental fonction', 'data':[{'x':1,'y':2}, {'x':3,'y':4}, {'x':5, 'y':6}]}
我希望我的csv导出是这样的:
name |x =>|1|2|3|5|
square fonction |y =>|1|4|9| |
incremental function|y =>|2| |4|6|
我宁愿使用Python
,但我对其他语言持开放态度。
也许我需要使用$unwind
运算符?
谢谢,
编辑:
这是一个短暂的步骤:我想更新文档,使它们像这样:
{'name': 'square fonction', 'data':[{'x':1,'y':1}, {'x':2,'y':4}, {'x':3, 'y':9}], 'data_updated':{y_from_x:{1:1, 2:4, 3:9}}}
{'name': 'incremental fonction', 'data':[{'x':1,'y':2}, {'x':3,'y':4}, {'x':5, 'y':6}], 'data_updated':{y_from_x:{1:2, 3:4, 5:6}}}
实际上,使用这些更新的文档,可以使用mongoexport命令将集合导出到csv。
答案 0 :(得分:2)
尝试使用$project运算符返回与您想要的类似的文档。也许您应该编写一些python脚本来将此结果格式化为csv文件。
AuthorIDs
你会得到两份文件:
db.test.aggregate
([
{ $project: { 'name':'$name' ,'y':'$data.y' ,'_id':0 } }
]);
希望这有帮助。
答案 1 :(得分:1)
这个解决方案看起来很难看。然而,它给出了想要的结果并解决了问题:
> //export to csv let me make assumption you want to use mongoexport, so we need auxilary collection:
> db.createCollection("f");
{ "ok" : 1 }
> //because you can have gaps in x values array, array does not work. Using object with named keys (to map y values to according x values later)
> //first we have to get all possible x values:
> var xa = new Object;
> db.test.find().forEach(function(o) {
... o.data.forEach(function(a) {
... xa[a.x] = a.x;
... });
... });
> var row = {name:"name"};
> var ak = Object.keys(xa);
> var map = new Object;
> ak.forEach(function(o,a,i) {row["x"+a] = o;});
> //now we create a map for y(x) relation:
> ak.forEach(function(o,a,i) {map[o] = "x"+a;});
> //pupolate first row:
> db.f.insert(row);
WriteResult({ "nInserted" : 1 })
> //now when we have all x values, we can "build" y row to fill:
> db.test.find().forEach(function(o) {
... var ya = {name:o.name};
... o.data.forEach(function(v,a,e) {
... ya[map[v.x]] = v.y;
... });
... db.f.insert(ya);
... });
> db.f.find();
{ "_id" : ObjectId("58a2c3e7fb1e9b689637660c"), "name" : "name", "x0" : "1", "x1" : "2", "x2" : "3", "x3" : "5" }
{ "_id" : ObjectId("58a2c3e7fb1e9b689637660d"), "name" : "square fonction", "x0" : 1, "x1" : 4, "x2" : 9 }
{ "_id" : ObjectId("58a2c3e7fb1e9b689637660e"), "name" : "incremental function", "x0" : 2, "x2" : 4, "x3" : 6 }
>
>
bye
vao@vao-VirtualBox:~$ mongoexport -c f --type=csv -f name,x0,x1,x2,x3
2017-02-14T08:46:54.133+0000 connected to: localhost
name,x0,x1,x2,x3
name,1,2,3,5
square fonction,1,4,9,
incremental function,2,,4,6
2017-02-14T08:46:54.134+0000 exported 3 records