简体   繁体   English

如何读取 csv 中的数组并将该数组存储在 mongodb 中

[英]how do I read array within csv and store that array within mongodb

This is my data.csv, there is cast column and its rows have array within it.这是我的数据。csv,有强制转换列,它的行里面有数组。

I want to retrieve that arrays from csv into mongodb.我想将 arrays 从 csv 检索到 mongodb 中。 I want to work with array within data.csv, but when i tried to import my csv into mongodb Compass, there is no datatype filter for array.我想使用 data.csv 中的数组,但是当我尝试将我的 csv 导入 mongodb Compass 时,没有数组的数据类型过滤器。 Moreover, I tried to write a code to read data from nodejs but using TYPEOF operator on data gave me output String.此外,我尝试编写代码来从 nodejs 读取数据,但是对数据使用 TYPEOF 运算符给了我 output 字符串。

I want to preserve my datatype of array.我想保留我的数组数据类型。

First you need to convert your csv file into an array of Objects and then upload that into mongo.首先,您需要将 csv 文件转换为对象数组,然后将其上传到 mongo。 You can use insertMany from mongoose to make this thing work您可以使用 mongoose 中的 insertMany 来完成此操作

[Collection-Name].insertMany(ArrayOfObj, { ordered: false, rawResult: true }, async 
function (err, result) {
 if(err) throw err;
 // do your thing
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM