MongoImport csv combine/concat 各种列到一个数组中进行导入
MongoImport csv combine/concat various columns to one array for import
我还有一个以前从未遇到过的有趣案例,所以我向 SO 社区寻求帮助并分享我的经验。
The case || What we have:
具有这种结构的csv
文件(从其他SQL数据库导出)
(headers):
ID,SpellID,Reagent[0],Reagent[1..6]Reagent[7],ReagentCount[0],ReagentCount[1..6],ReagentCount[7]
您还可以在此处查看完整的 -csv
数据文件,在我的
保管箱
My gist from Github,这有助于您了解 MongoImport
的工作原理。
What we need:
我想接收这样的结构(模式)以将其导入 MongoDB
collection:
ID(Number),SpellID(Number),Reagent(Array),ReagentCount(Array)
6,898,[878],[1]
使用 ID、SpellID 和两个数组,首先我们存储所有试剂 ID,例如来自所有 Reagent[n]
列的 [0,1,2,3,4,5,6,7],以及在第二个数组中,我们有一个长度相同的数组,代表 quantity
个 ReagentID,来自所有 ReagentCount[n]
或
具有这种结构(架构)的转置 objects:
ID(Number),SpellID(Number),ReagentID(Number),Quantity/Count(Number)
80,2675,1,2
80,2675,134,15
80,2675,14,45
如您所见,第一个示例与本示例的区别在于,collection 中的每个文档代表每个 ReagentID
,其数量为 SpellID
。因此,如果一个 Spell_ID
有 N
种不同的试剂,它将在 collection 中有 N
个文件,因为我们都知道,不能超过 7 个独特的 [=根据我们的 -csv
文件,26=] 属于一个 Spell_ID
。
I am working on this problem right now, with the help of node js
and npm i csv
(or any other modules for parsing csv
files). Just to make my csv
file available for importing to my DB via mongoose
. I'll be very thankful for all those, who could provide any relevant contribution to this case. But anyway, I will solve this problem eventually and share my solution in this question.
As for the first variant I guess there should be one-time script for MongoImport that could concat all columns from Reagent[n]
& ReagentCount[n]
to two separate arrays like I mentioned above, via -fields but unfortunately I don't know it, and there are no examples on SO or official Mongo docs relevant to it. So if you have enough experience with MongoImport
feel free to share it.
最后我解决了我的问题,但没有使用 mongoimport
我使用 npm i csv
和 write 函数来解析我的 csv
文件。简而言之:
async function FuncName (path) {
try {
let eva = fs.readFileSync(path,'utf8');
csv.parse(eva, async function(err, data) {
//console.log(data[0]); we receive headers, if they exist
for (let i = 1; i < data.length; i++) { //we start from 1, because 0 is headers, if we don't have it, then we start from 0
console.log(data[i][34]); //where i is row number and j(34) is a header address
}
});
} catch (err) {
console.log(err);
}
}
它遍历 csv
文件并在数组中显示数据,您可以根据需要对它们进行操作。
我还有一个以前从未遇到过的有趣案例,所以我向 SO 社区寻求帮助并分享我的经验。
The case || What we have:
具有这种结构的
csv
文件(从其他SQL数据库导出) (headers):ID,SpellID,Reagent[0],Reagent[1..6]Reagent[7],ReagentCount[0],ReagentCount[1..6],ReagentCount[7]
您还可以在此处查看完整的
-csv
数据文件,在我的 保管箱My gist from Github,这有助于您了解
MongoImport
的工作原理。
What we need:
我想接收这样的结构(模式)以将其导入 MongoDB
collection:
ID(Number),SpellID(Number),Reagent(Array),ReagentCount(Array)
6,898,[878],[1]
使用 ID、SpellID 和两个数组,首先我们存储所有试剂 ID,例如来自所有 Reagent[n]
列的 [0,1,2,3,4,5,6,7],以及在第二个数组中,我们有一个长度相同的数组,代表 quantity
个 ReagentID,来自所有 ReagentCount[n]
或
具有这种结构(架构)的转置 objects:
ID(Number),SpellID(Number),ReagentID(Number),Quantity/Count(Number)
80,2675,1,2
80,2675,134,15
80,2675,14,45
如您所见,第一个示例与本示例的区别在于,collection 中的每个文档代表每个 ReagentID
,其数量为 SpellID
。因此,如果一个 Spell_ID
有 N
种不同的试剂,它将在 collection 中有 N
个文件,因为我们都知道,不能超过 7 个独特的 [=根据我们的 -csv
文件,26=] 属于一个 Spell_ID
。
I am working on this problem right now, with the help of
node js
andnpm i csv
(or any other modules for parsingcsv
files). Just to make mycsv
file available for importing to my DB viamongoose
. I'll be very thankful for all those, who could provide any relevant contribution to this case. But anyway, I will solve this problem eventually and share my solution in this question.As for the first variant I guess there should be one-time script for MongoImport that could concat all columns from
Reagent[n]
&ReagentCount[n]
to two separate arrays like I mentioned above, via -fields but unfortunately I don't know it, and there are no examples on SO or official Mongo docs relevant to it. So if you have enough experience withMongoImport
feel free to share it.
最后我解决了我的问题,但没有使用 mongoimport
我使用 npm i csv
和 write 函数来解析我的 csv
文件。简而言之:
async function FuncName (path) {
try {
let eva = fs.readFileSync(path,'utf8');
csv.parse(eva, async function(err, data) {
//console.log(data[0]); we receive headers, if they exist
for (let i = 1; i < data.length; i++) { //we start from 1, because 0 is headers, if we don't have it, then we start from 0
console.log(data[i][34]); //where i is row number and j(34) is a header address
}
});
} catch (err) {
console.log(err);
}
}
它遍历 csv
文件并在数组中显示数据,您可以根据需要对它们进行操作。