1. 什么是数据库
数据库:存储数据的仓库,可以将数据进行有序的分类别的存储。它是独立于语言之外的软件,可以通过 API 去操作它。
常见的数据库软件有:mysql, mongoDB, oracle
2. mongoDB
数据库下载安装
下载地址:https://www.mongodb.com/download-center/community
3. mongoDB
可视化软件
MongoDB可视化操作软件,是使用图形界面操作数据库的一种方式。
4. 数据库相关概念
在一个数据库软件中可以包含多个数据仓库,在每个数据仓库中可以包含多个数据集合,每个数据集合中可以多条文档。
databases --> 数据库, mongoDB数据库软件可以创建多个数据库
collection --> 集合, 一组数据的集合, 可以理解为 javascript中的数组
document --> 文档, 一条具体的数据,可以理解为javascript 中的对象
field --> 字段, 文档中的属性名称, 可以理解为javascript中的对象属性
5. 启动 mongoDB
数据库
net start mongodb
net stop mongodb
6. 使用mongoose
提供的 connect
方法连接 mongoDB
数据库
npm install mongoose --save
const mongoose = require('mongoose')
mongoose.connect('mongoDB:://localhost/studentInfo', {useNewUrlParser:true})
.then(() => {
console.log('数据库连接成功。')
}).catch(err => {
console.log('数据库连接失败。')
})
const studentSchema = new mongoose.Schema({
name: String,
age: Number,
love: [Sting],
isChecked: Boolean
})
const Student = mongoose.modle('Student', studentSchema)
const student = new Student({
name: "王曦",
age: 25,
love: ['爬山', '乒乓球'],
ischecked: false
})
student.save()
6.1 创建集合
const studentSchema = new mongoose.schema({
name: Stirng,
age: Number,
address: String
})
const Student = mongoose.modle('Student', studentSchema)
6.2.向mongoDB
添加数据
const studentSchema = new mongoose.Schema({
name: String,
age: Number,
isShow: Boolean
})
const Student = mongoose.model('Student', studentSchema)
const student = new Student({
name: "王欣",
age: 25,
isShow: true
})
student.save()
const studentSchema = new mongoose.Schema({
name: String,
age: Number,
isShow: Boolean
})
const Student = mongoose.model('Student', studentSchema)
Student.create({
name: "王欣",
age: 25,
isShow: false
}, (err, data) => {
console.log(data)
})
Student.create(插入的对象数据).then(data => {}).catch(err => {})
6.3 mongoDB
数据库导入数据
mongoimport -d student -c user --file ./user.json
7. 从数据库中查找数据
Student.find({}).then(result => {console.log(result)})
[{
name: '王欣',
age: 25,
isShow: false
}, {
name: "李明轩",
age: 26,
isShow: true
}]
Students.find({_id: "ef3dds32"}).then(result => {console.log(result)})
Student.findOne({条件}).then(result => {console.log(result)})
const mongoose = require('mongoose')
mongoose.connect("mongodb://localhost/student")
.then(() => {
console.log("数据库连接成功!")
})
.catch(() => {
console.log("数据库连接失败!")
})
const studentSchema = new mongoose.Schema({
name: String,
age: Number,
address: String,
hobbies: [String]
})
const Student = mongoose.model("Student", studentSchame)
Student.find({name: '李希'}).then((result) => {
console.log(result)
})
7.1 文档查询
Student.find({age: {$gt:20, $lt: 50}}).then(result => {console.log(result)})
Student.find({hobbies: {$in:['跳舞']}}).then(result => {console.log(result)})
Student.find().select('name', 'age').then(result => {console.log(result)})
Student.find().sort('age').then(result => {console.log(result)})
Student.find().sort('-age').then(result => {console.log(result)})
8. 从数据库中删除数据
Student.findOneAndDelete({}).then(result => {console.log(result)})
Student.findOneAndDelete({_id:"fewadfwer2421faf"}).then(result => {console.log(result)})
Student.deleteMany({}).then(result => {console.log(result)})
9. 更新数据库中的数据
Student.updateOne({查询条件}, {要修改的值}).then(result => {console.log(result)})
Student.undateOne({name: '李希'}, {name: '王曦'}).then(result => {console.log(result)})
Student.updateMany({查询条件}, {要修改的值}).then(result => {console.log(result)})
Student.updateMany({}, {age: 56}).then(result => {console.log(result)})
10 monogose
字段验证
1. require: true
2. minlength: 3
3. maxlength: 20
4. min: 2
5. max: 100
6. trim: true
7. default
8. enum: ['html', 'css', 'node']
9. validate: {
validator: v => {
}
}
mongoose.connect("mongodb://localhost/student", {useNewUrlParser: true})
.then(() => { console.log("数据库连接成功!") })
。catch(() => { console.log("数据库连接失败!") })
const studentSchema = new mongoose.Schema({
name: {
type: String,
require: [true, '请输入名字'],
minlength: 2,
maxlength: 10
},
title: {
type: String,
require: [true, '请输入文章标题'],
minlength: [2, '标题的长度不能小于2'],
maxlength: [20, '标题的长度不能大于20'],
trim: true
},
publiceDate: {
type: Date,
default: Date.now
},
category: {
type: String,
enum: {
value: ['html', 'css', 'javascript', 'node'],
message: "分类名称要在一定的范围内才可以。"
}
},
author: {
type: String,
validate: {
validator: v => {
return v && v.length > 4
}
},
message: "传入的值不符合验证规则"
}
})
const Student = mongoose.model('Student', studentSchema)
Student.create({ name: '王欣' }).then((result) => { console.log(result) })
3.7 集合关联
const User = mongoose.model('User', new mongoose.schema({
name: {
type: String
}
}))
const Post = mongoose.model('Post', new mongoose.shema({
title: {
type: String,
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User
}
}
}))
Post.find().populate('author').then((err, result) => { console.log(result) })