mongoose.connect(‘mongodb://localhost/playground‘)
.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) // courses
// 创建文档
const course = new Course({
name: ‘node.js基础‘,
author: ‘Jannik‘,
isPublished: true
});
// 将文档插入到数据库中
course.save();
原文:https://www.cnblogs.com/Jannik/p/14472382.html