Skip to content
js
// 添加数据,多余的字段不会被添加进去。
;(async () => {
  try {
    // 方式1:往 Student 里添加一个 student
    const res = await Student.create({
      name: 'foo',
      age: 18,
      hobby: 'bar',
      baz: 'baz',
    })
    // 方式2:创建一个 student,添加进 Student 里。
    const res2 = await new Student({ name: 'foo' }).save()

    // 批量添加
    const data = []

    for (let i = 0; i < 20; i++) {
      data.push({
        name: 'foo',
        age: 18,
      })
    }

    const res3 = await Student.create(data)
  } catch (error) {
    console.log(error)
  }  

  conn.close()
})()