简体   繁体   English

如何在不知道 model 的情况下将 JSON 数组批量插入 mysql 数据库并进行续集?

[英]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.

相关问题 在mysql Sequelize + Nodejs中将数组和对象转换为大容量插入 - Conversion of Array and objects to bulk insert in mysql Sequelize + Nodejs 如何在NodeJS中的MySQL中批量插入JSON对象数组 - How to bulk insert JSON array of objects in MySQL in NodeJS 如何在 mysql 中使用 foreach 插入查询批量插入数据库 - How to bulk insert to database using foreach insert query in mysql 如何在不知道其索引的情况下删除嵌套数组? - How do I delete a nested array without knowing its index? 如何批量插入对象数组并在 NodeJS 中插入 MySQL - How to bulk insert array of objects and insert MySQL in NodeJS Javascript(NodeJs)-将数组和对象转换为在mysql中批量插入(Sequelize) - Javascript(NodeJs) - Conversion of Arrays and Objects to bulk insert in mysql(Sequelize) Angular & Firebase 实时数据库。 如何让孩子知道其 id 属性但不知道其父节点 - Angular & Firebase Realtime-Database. How to get child knowing its id property but without knowing its parent node Javascript:如何在不知道键名的情况下解析json数组? - Javascript: How to parse a json array without knowing the key name? 如何将JSON数组插入数据库 - How to insert JSON Array into database 如何创建Sequelize模型实例,而不将其保存在数据库中? - How to create a Sequelize model instance, without saving it in the database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM