0

我有以下 CSV:

matchId,       score, players.Name, players.Goals
2730319610399, 5-0,   John,         3

当我在 Studio 3T 上使用 mongoimport 时,由于点表示法,它以我需要的形式导入:

{ 
    "matchId" : "2730319610399", 
    "score" : "5-0", 
    "players" : {
        "Name" : "John", 
        "Goals" : "3"
    }
}

我的问题是 csv 实际上还有一个我想在此导入中添加的播放器。“玩家”数组有两个条目。

这是实际的 CSV 格式:

matchId,       score, players.Name, players.Goals, players.Name, players.Goals
2730319610399, 5-0,   John,         3,             Kyle,         2

但这不起作用,我收到以下错误:

Every row will be parsed as one column.

The header contains a duplicate name "players.Name" in columns: [3, 5]

是否可以格式化 CSV,以便我可以将多个值添加到“玩家”数组中?我正在考虑将其命名为 player[0].Name 和 player[1].Name

但这不起作用,因为它创建了两个数组:players[0] 和 player[1]

这就是我需要的数据库结构:

{ 
    "matchId" : "2730319610399", 
    "score" : "5-0", 
    "players" : {
        "Name" : "John", 
        "Goals" : "3"
    },
    {
        "Name" : "Kyle", 
        "Goals" : "2"
    }
}
4

2 回答 2

0

好的,所以最好的选择是导入 json 文件而不是 csv。例如:

{ "matchId":"2730319610399","score":"5-0","players":[{"Name":"John","Goals":"3"},{"Name":"Kyle","Goals":"2"}]}
{ "matchId":"2830319610399","score":"1-0","players":[{"Name":"Mauri","Goals":"1"}]}

然后使用 mongoimport 如下:

mongoimport --db=TestDB --collection=TestCol --type=json example_file.json

你应该在 Robo3T 上看到类似这样的内容:

robo3t_mongodb

于 2020-12-28T13:29:02.747 回答
0

试试这个:

matchId,    score,     players.Name,     players.Goals

2730319610399,    5-0,    John.Kyle,             3.2

然后,使用:

db.collection.find().snapshot().forEach(function (test) {
   test.players.Name = test.players.Name.split('.');  
 db.whatevercollection.save(test); 
});

db.collection.find().snapshot().forEach(function (test) {
   test.players.Goals = test.players.Goals.split('.');  
 db.whatevercollection.save(test); 
});
于 2020-12-23T13:28:41.890 回答