我正在尝试通过nodejs将csv文件导入MongoDB;问题是(我想)csv的每一行中都有一个Windows CRLF
在MongoDB中,mongoimport创建了一个“:”作为每个文档的最后一个字段:
这在继续这一进程方面造成了许多问题;我尝试了这个代码,但它没有解决,我不明白为什么(如果我打开csv,我只有一个LF):
export async function replaceCarriageReturnLineFeed(filePath: string) {
try {
const fileRead = await readFilePromise(filePath, {
encoding: "utf8"
});
if (fileRead) {
const result = fileRead.replace(/\r\n/g, "\n").replace(/\"/g, "");
_log.info(`Characters replaced`);
await writeFilePromise(filePath, result, "utf8");
}
return true;
} catch (error: any) {
throw new Error(
`Unable to replace carriage return line feed in file ${filePath} - ${error.message}`
);
}
}
而且,mongoimport是:
await importData(
join(zipDir, data_file_name),
code,
"csv",
collectionName,
false,
true
);
需要替换“”,因为否则我会将数字作为字符串,而不是MongoDB中的Double。
该文件可在以下链接中找到:
https://databank.worldbank.org/data/download/WDI_CSV.zip
里面有WDIData.csv
使用$unset是不可能的;不能删除“:”对。