2、插入数据的另一种方法

const mongoose=require('mongoose')
//链接数据库,成功运行then里面的代码,错误运行catch里面的代码
mongoose.connect("mongodb://localhost/playground",{ useNewUrlParser: true,useUnifiedTopology: true})
.then(()=>{console.log("数据库连接成功")})
.catch(err=>{console.log(err,'数据库失败')})
//创建集合规则,返回构造函数
const courseSchema= new mongoose.Schema({
    name:String,
    author:String,
    isPublished:Boolean
});
//使用规则创建集合,参数1是集合名,2是规则
const Course= mongoose.model("Course",courseSchema)
//第二种创建数据的方法
Course.create({name:'javascript',author:'ZG',isPublished:false},(err,result)=>{
    console.log(err)
    console.log(result)
})
原文地址:https://www.cnblogs.com/tilyougogannbare666/p/14462975.html