如何在节点js中使用sequelize和mysql插入批量数据,而不使用循环进行插入查询

时间:2017-11-15 16:28:32

标签: mysql insert sequelize.js bulk

如何将数组或json插入表中? array或json包含所有字段和值。

{
 "userId":24,
 "addressList":[{ "city":"delhi",
                  "state":"delhi"
                },
                { "city":"gurugram",
                 "state":"hariyana"
                },
                { "city":"dehradun",
                  "state":"uttrakhand"
                }]
}

表详情: userId城市州 24德里德里 24 Gurugram Hariyana 24 Dehradun uttrakhand

1 个答案:

答案 0 :(得分:0)

在续集中使用bulkCreate()函数。

 User.bulkCreate([
   { username: 'barfooz', isAdmin: true },
  { username: 'foo', isAdmin: true },
  { username: 'bar', isAdmin: false }
]).then(() => { // Notice: There are no arguments here, as of right now you'll have to...
  return User.findAll();
}).then(users => {
  console.log(users) // ... in order to get the array of user objects
})

这是一个示例示例。 Refer to this for bulk create docs