1、什么是数据库
数据库是存储数据的仓库,可以讲数据进行有序的分门别类的存储,它是独立语言之外的软件,可以通过api去操作它
常见的数据库软件有 : mysql 、 mongoDB、oracle
2、MongoDB数据库下载安装
下载地址:https://www.mongodb.com/download-center/community
工具下载:https://www.mongodb.com/try/download/database-tools
3、数据库了解
4、mongoose第三方包
npm install mongoose命令下载
5、启动MongoDB数据库
在命令行工具运行 net start mongDB
6、数据库连接
使用moogoose提供的connect方法连接数据库。
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/playground')
.then(() => console.log('数据库连接成功'))
.catch(error => console.log(error, '数据库连接失败'))
运行node 01.js 就报错了 说这样子不行需要加上以下参数不然会报异常
完整代码如下
// 引入mongoose第三方模块 用来操作数据库
const mongoose = require('mongoose')
// 数据库连接
mongoose.connect('mongodb://localhost:27017/playground', {
useNewUrlParser: true,
useUnifiedTopology: true
})
// 连接成功
.then(() => console.log('数据库连接成功'))
// 连接失败
.catch(error => console.log(error, '数据库连接失败'))
7、创建集合规则
// 创建集合规则
const courseSchema = new mongoose.Schema({
name: String,
author: String,
isPublished: Boolean
})
// 使用规则创建集合
// 1、集合名称
// 2、集合规则
const Course = mongoose.model('Course', courseSchema) //courses
// 获取异步api
// Course.create({ name: 'Javascript', author: '黑马讲师', isPublished: false }, (err, result) => {
// // 错误对象
// console.log(err);
// // 当前插入文档
// console.log(result);
// })
// 通过构造函数插入数据
Course.create({ name: '小程序', author: '黑马讲师', isPublished: true })
.then(result => {
console.log(result);
}).catch(err => {
console.log(err);
})
8、mongoDB数据库导入数据
mongoimport -d 数据库名称 -c 集合名称 --file 要导入的文件
在文件夹下新增user.json 静态数据文件
{"name": "王五", "age": 22, "hobbies": ["听歌","画画"], "email": "27@qq.com", "password": "123456"}
{"name": "李四", "age": 18, "hobbies": ["运动","弹琴"], "email": "26@qq.com", "password": "643521"}
{"name": "张三", "age": 26, "hobbies": ["烘培","敲代码"], "email": "25@qq.com", "password": "345621"}
{"name": "二货", "age": 19, "hobbies": ["听歌","吃饭"], "email": "24@qq.com", "password": "234561"}
{"name": "王者", "age": 8, "hobbies": ["做饭","敲代码","打豆豆"], "email": "23@qq.com", "password": "324561"}
{"name": "荣耀", "age": 32, "hobbies": ["乒乓球"], "email": "22@qq.com", "password": "235641"}
{"name": "吃鸡", "age": 33, "hobbies": ["篮球"], "email": "21@qq.com", "password": "145623"}
{"name": "战场", "age": 24, "hobbies": ["吃货"], "email": "11@qq.com", "password": "981234"}
注意每条数据必须在同一行,然后执行命令导入 发现报异常了找不到后来找了方法
【原因】
打开MongoDB安装目录bin文件夹,发现没有mongoimport.exe这个程序
【解决方法】
1.到官网下载压缩包
https://www.mongodb.com/try/download/database-tools?tck=docs_databasetools
2.解压后把bin文件夹里的文件全部拷贝到MongoDB安装目录bin文件夹下在运行 mongoimport -d playground -c users --file ./user.json 这个命令就可以正常导入数据了
9、查询文档find()、findOne()
// 查询用户集合中的所有文档
User.find().then(res => console.log(res))
// 根据指定id 获取数据
User.find({ _id: '60093edfacc33ed1e296eb48' }).then(res => console.log(res))
// findOne方法返回一条文档 默认返回当前集合中的第一条文档
User.findOne({ name: '李四' }).then(res => console.log(res))
// 匹配用户年龄大于20 小于50
User.find({ age: { $gt: 20, $lt: 50 } }).then(res => console.log(res))
// 匹配包含 $in
User.find({ hobbies: { $in: ["敲代码"] } }).then(res => console.log(res))
// 选择要查询的字段
User.find().select('name email -_id').then(res => console.log(res))
// 根据年龄升序排序
User.find().sort('age').then(res => console.log(res))
// 根据年龄降序排序
User.find().sort('-age').then(res => console.log(res))
// skip 跳过多少条数据 limit 限制查询数量
User.find().skip(2).limit(3).then(res => console.log(res))
10、 删除文档
// 查找一条数据并且删除
// 返回删除的文档
// 如何查询匹配了多个文档 那么将会删除第一个匹配的文档
User.findOneAndDelete({ _id: "600944c3f760f093f8989016" }).then(res => console.log(res))
// 删除全部数据
// 返回ok:1 删除成功 deletedCount:7 当前删除了7条数据
User.deleteMany({}).then(res => console.log(res))
11、更新文档
// 更新集合中的文档
User.updateOne({ name: '李四' }, { name: '李狗蛋' }).then(res => console.log(res))
// 更新多个集合中的文档
User.updateMany({}, { age: 56 }).then(res => console.log(res))