const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/playground', { useUnifiedTopology: true }).then(() => console.log('数据库连接成功')).catch(err => console.log(err, '数据库连接失败'))//创建集合规则
const courseSchema = new mongoose.Schema({name: String,author: String,isPublished: Boolean
});
//使用集合并应用规则
const Course = mongoose.model('Course', courseSchema);//创建实例
const course = new Course({name: 'node.js',author: 'geyao',isPublished: true
});
course.save();//向结合中插入文档
/* Course.create({name:'javascript',author:'hema',isPublished:false},(err,result)=>{console.log(err)console.log(result)
}) */
Course.create({ name: 'javascript123', author: 'hema', isPublished: false }).then(result => {console.log(result)})
运行结果