美文网首页nodeJs
nodejs--day5笔记

nodejs--day5笔记

作者: Grandperhaps | 来源:发表于2020-02-03 18:24 被阅读0次

    数据库

    1. 创建集合

    微信截图_20200203155750.png
    const 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.png

    6. 更新文档

    微信截图_20200203182351.png
    微信截图_20200203182412.png

    相关文章

      网友评论

        本文标题:nodejs--day5笔记

        本文链接:https://www.haomeiwen.com/subject/brwqxhtx.html