[英]How to Bulk insert JSON array into mysql database with sequelize without knowing its model?
I have a JSON array of more than 50000+ records.我有一个包含超过 50000 条记录的 JSON 数组。 is there any way I can use sequelize to bulk insert the dynamic data into the database as there is no model for the sequelize object?
有什么方法可以使用续集将动态数据批量插入数据库,因为续集 object 没有 model?
If I understood correctly, you have a large array of json objects that you want to insert into mysql.如果我理解正确,您有大量 json 对象要插入到 mysql 中。 Assuming the JSON structure is the same for all elements of the array, you can use
sequelize.query
after manipulating the objects into proper arrays for column names and values:假设 JSON 结构对于数组的所有元素都是相同的,您可以在将对象操作为适当的
sequelize.query
后使用 sequelize.query 来获取列名和值:
const colNames = Object.keys(bigArrayOfObjects[0]).map(elem => '`' + elem + '`').join(', ')
const rowValues = bigArrayOfObjects.map(row => Object.values(obj))
await sequelize.query(`
INSERT INTO company (${colNames}) VALUES ${values.map(() => '(?)').join(',')}
`, {
replacements: values,
type: Sequelize.QueryTypes.INSERT,
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.