数据库
1. 创建集合
微信截图_20200203155750.pngconst courseSchema = new mongoose.Schema({
name:String,
author: String,
isPublised: Boolean
});
//使用规则创建集合
const Course = mongoose.model('Course',courseSchema) //course
2. 创建文档
微信截图_20200203162544.png//引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
//数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true})
.then(() => console.log('连接成功'))
.catch(err => console.log(err,'连接失败'));
//创建集合规则
const courseSchema = new mongoose.Schema({
name:String,
author: String,
isPublised: Boolean
});
//使用规则创建集合
const Course = mongoose.model('Course',courseSchema) //course
const course = new Course({
name:'hhh',
author:'袁航',
isPublised:true
});
course.save();
create方法
微信截图_20200203175336.png//引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose');
//数据库连接
mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true})
.then(() => console.log('连接成功'))
.catch(err => console.log(err,'连接失败'));
//创建集合规则
const courseSchema = new mongoose.Schema({
name:String,
author: String,
isPublised: Boolean
});
//使用规则创建集合
const Course = mongoose.model('Course',courseSchema) //course
Course.create({name:'123',author:'yh',isPublised: true},(err,result) =>{
console.log(err)
console.log(result)
})
3.导入数据
微信截图_20200203180453.png
微信截图_20200203180505.png
4.查询数据
微信截图_20200203181117.png 微信截图_20200203181133.png微信截图_20200203181611.png
5.删除文档
微信截图_20200203182208.png6. 更新文档
微信截图_20200203182351.png微信截图_20200203182412.png
网友评论