数据库开坑mongoDB---day1

//引入mongoose数据库
const mongoose=require('mongoose');
//链接数据库,格式固定(mongodb://~~),没有会帮你自动创建//下面会出现一些关于未来版本的警告,按他们说的复制粘贴代码块就好了
mongoose.connect('mongodb://localhost/playground',{useNewUrlParser:true,useUnifiedTopology: true } )
        .then(()=>{console.log('数据库连接成功');})
        .catch((err)=>{console.log(err,'数据库连接失败');});

//引入mongoose数据库
const mongoose=require('mongoose');
//链接数据库,格式固定(mongodb://~~),没有会帮你自动创建//下面会出现一些关于未来版本的警告,按他们说的复制粘贴代码块就好了
mongoose.connect('mongodb://localhost/playground',{useNewUrlParser:true,useUnifiedTopology: true } )
        .then(()=>{console.log('数据库连接成功');})
        .catch((err)=>{console.log(err,'数据库连接失败');});

//创建集合规则mongoose.Schema
const courseschema = new mongoose.Schema({
    name: String,
    author:String,
    ispublish:Boolean
});

//使用规则创建集合,规定首字母大写,但是在数据库里的名字实际为courses
const Course = mongoose.model('Course',courseschema)    //courses
const course1= new Course({    //实例化,返回对象
    name:'Eille',
    author:`hk${400+16}`,//这样也是可以滴
    ispublish:true
})
//插入一条数据
course1.save();
//之后就会在mongoDB的app里发现新建文件夹’playground‘中有数据了
//!!改变程序中的值并且重新输入执行,会另外生成一个不同id的同类型数据
//这里感觉要注意下

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值