MongDB笔记

MongoDB

MongoDB的下载安装

安装路径 https://www.mongodb.com/try/download/community

可视化软件 https://www.mongodb.com/try/download/compass

通过node下载第三方模块以便使用node进行操作 npm install -g mongoose

数据库所有操作都是异步操作

连接数据库

//导入mongoose链接数据库模块
const mongoose = require('mongoose')
//链接数据库
mongoose.connect("mongodb://localhost/playground",{useNewUrlParser: true,useUnifiedTopology: true})
//链接成功走.then的方法
.then(() => console.log('数据库链接成功'))
//链接失败走的时.catch方法
.catch(err => console.log(err,'数据库链接失败'))

创建集合

//创建集合规则
const 变量名 = new mongoose.Schema({
	集合字段名1: 类型,
	集合字段名2: String,
	集合字段名3: Boolean
})

//使用规则创建函数
const 变量名 = mongoose.model('集合名称',规则名称)

创建内容插入内容1

//创建内容
const course = new Course({
	name: "node.js基础",
	author: "黑马",
	isPublished: true
})

//保存数据
course.save()

创建内容插入内容2

//创建文档的第二种办法
Course.create({name:'javascript',author:'黑马讲师',isPublished:false},(err,doc) => {
	//输出错误信息
	console.log(err)
	//输出文档内容
	console.log(doc)
})

.then这个支持异步

向数据库导入数据

导入数据     mongoimport -d 数据库名称 -c 集合名称 -file 要导入的数据文件

数据查找

命令代码
找到所有内容find()
根据条件进行查找findOne({name:‘张先生’})
查找区间范围find({age:{gt:10,gt:10,gt:10,lt:30}})
查询包含内容find({xingqu:{$in:[‘蹦极’]}})
跳过查询skip(跳过的数据个数)
限制查询limit(查询的数据个数)
删除文档findOneAndDelete(通过查询要删除的文档)
全部删除deleteMany({})

find返回一组,findOne返回一个

查询文档
// 查询用户集合中的所有文档
 User.find().then(result => console.log(result));

// 通过_id字段查找文档
User.find({_id:'60dbc5fad017d9a52c510ca1'}).then(result => console.log(result))

// findOne方法撒返回一条文档 默认返回当前集合中的第一条文档
User.findOne({name:'张1三'}).then(result => console.log(result))

//  查询用户集合中年龄字段大于小于的文档       
User.findOne({age:{ $gt:40 , $lt:60}}).then(result => console.log(result)) 

// 包含
User.find({hobbies:{$in: ['吃饭']}}).then(result => console.log(result))

// 选择要查询的字段 select 不想被查询出 -_id
User.find().select('name email -_id').then(result =>console.log(result))

// 根据年龄字段进行升序排列
User.find().sort('age').then(result => console.log(result))

// 根据年龄字段进行降序排列
User.find().sort('-age').then(result => console.log(result))
删除文档
// 查找到一条文档并且删除
// 返回删除的文档
// 如果查询条件匹配了多个文档 那么将会删除第一个匹配的文档
User.findOneAndDelete({_id:'60dbccfa6ff160e91412e046'}).then(resule => console.log(result))

// 删除多个文档 返回值n是数量 ok:1 成功
User.deleteMany({}).then(result => console.log(result))
更新文档
// 更新集合中的文档(更新一个)
User.updateOne({name:'张1三'},{name:'张狗蛋'}).then(result => console.log(result))

// 更新集合中的文档(更新多个) n 代表受影响的数据 nModified 更改了几条
User.updateMany({},{age:56}).then(result => console.log(result)) 

数据判断设置 mongoose验证

设置不可以为空     required: [true,'不能为空']
设置最小长度     minlength: [2,'长度不得小于两位']
设置最大长度     maxlength: [5,'长度不得大于五位']
清除两边空格     trim: true
设置数字的最小值     min: 2
设置数字的最大值     max: 100
设置日期格式的默认时间     default: Date.now
设置当前字段可拥有的值     enum: ['html','css','javascript']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值