mongodb
# mongodb
# 环境变量配置
配置完环境变量可直接在dos窗口通过命令启动
C:\Program Files\mongodb-win32-x86_64-windows-5.0.17\bin
# 启动命令
服务端启动命令:mongod
客户端启动命令:mongo
# 文档相关命令
1.插入文档
db.集合.insert(文档对象)
1
2.查询文档
db.集合.find(查询条件)
1
3.更新文档
db.集合.update(查询条件,新的文档)
db.集合.update({name:'张三'},{$set:{age:19}})
1
2
2
4.删除文档
db.集合.remove(查询条件)
1
# 应用场景
- 新增
- 删除 (伪删除)
- 更新
- 查询
# Mongoose(方便操作数据库)
谨记一句话,永远不要相信用户的输入
官方文档:http://www.mongoosejs.net/docs/models.html
# 插入文档
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/bilibili')
// once 重新连接不会再执行
// on 断开 重新连接会再执行
mongoose.connection.once('open', () => {
console.log('成功');
// 创建文档对象
// 设置集合文档属性以及属性值的类型
let BookSchema = new mongoose.Schema({
name: String,
author: String,
price: Number
})
// 创建模型对象 对文档操作的封装对象
// 新版需要支持promise的回调
let model = mongoose.model('info', BookSchema)
model.create({
name: '孙悟空',
author: '吴承恩',
price: 99
}).then((res) => {
console.log(res);
})
})
//成功回调
mongoose.connection.on('error', () => {
console.log('失败');
}) //失败回调
mongoose.connection.on('close', () => {
console.log('关闭');
}) //关闭回调
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 字段类型
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/bilibili')
// once 重新连接不会再执行
// on 断开 重新连接会再执行
mongoose.connection.once('open', () => {
console.log('成功');
// 创建文档对象
// 设置集合文档属性以及属性值的类型
let BookSchema = new mongoose.Schema({
name: String,
author: String,
price: Number,
is_hot: Boolean,
tags: Array,
date: Date,
buffer: Buffer,
mixed:mongoose.Schema.Types.Mixed
})
// 创建模型对象 对文档操作的封装对象
let model = mongoose.model('info', BookSchema)
model.create({
name: '孙悟空',
author: '吴承恩',
price: 99,
is_hot: true,
tags: ['大师兄', '二师兄', '三师弟'],
date: new Date(),
}).then((res) => {
console.log(res);
})
})
//成功回调
mongoose.connection.on('error', () => {
console.log('失败');
}) //失败回调
mongoose.connection.on('close', () => {
console.log('关闭');
}) //关闭回调
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 字段验证
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/bilibili')
// once 重新连接不会再执行
// on 断开 重新连接会再执行
mongoose.connection.once('open', () => {
console.log('成功');
// 创建文档对象
// 设置集合文档属性以及属性值的类型
let BookSchema = new mongoose.Schema({
name: {
type: String,
required: true, // 该属性必须不为空
unique: true, // 设置为独一无二 唯一值 unique需要重建集合
},
style: {
type: String,
// 枚举
enum: ['言情', '戏剧', '恐怖', '开心']
},
author: String,
price: Number,
is_hot: Boolean,
tags: Array,
date: Date,
})
// 创建模型对象 对文档操作的封装对象
let model = mongoose.model('info', BookSchema)
model.create({
name: '西游记',
author: '吴承恩',
price: 99,
is_hot: true,
tags: ['大师兄', '二师兄', '三师弟'],
date: new Date(),
}).then((res) => {
console.log(res);
}).catch((err) => {
console.log('插入失败', err);
})
})
//成功回调
mongoose.connection.on('error', () => {
console.log('失败');
}) //失败回调
mongoose.connection.on('close', () => {
console.log('关闭');
}) //关闭回调
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 删除文档
let model = mongoose.model('info', BookSchema)
// 删除单个 删除id为12的数据
model.deleteOne({ id: 12 }, (err, res) => {
if (err) {
console.log('删除失败');
return
}
console.log(res);
})
// 删除多个 删除is_hot为false的数据
model.deleteMany({ is_hot: false }, (err, res) => {
if (err) {
console.log('删除失败');
return
}
console.log(res);
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 更新文档
const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/bilibili')
// once 重新连接不会再执行
// on 断开 重新连接会再执行
mongoose.connection.once('open', () => {
console.log('成功');
// 创建文档对象
// 设置集合文档属性以及属性值的类型
let BookSchema = new mongoose.Schema({
name: {
type: String,
required: true, // 该属性必须不为空
// default: '无名人',
unique: true, // 设置为独一无二 唯一值
},
style: {
type: String,
// 枚举
enum: ['言情', '戏剧', '恐怖', '开心']
},
author: String,
price: Number,
is_hot: Boolean,
tags: Array,
date: Date,
})
// 创建模型对象 对文档操作的封装对象
let model = mongoose.model('info', BookSchema)
model.updateOne({ name: '红楼梦' }, { price: 9.9 }, (err, data) => {
if (err) {
console.log('更新失败');
return
}
console.log(data);
})
model.updateMany({ author: '余华' }, { is_hot: true }, (err, data) => {
if (err) {
console.log('更新失败');
return
}
console.log(data);
})
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 读取文档
// 创建模型对象 对文档操作的封装对象
let model = mongoose.model('info', BookSchema)
// 读取单挑
model.findOne({ name: '孙悟空' }, (err, data => {
if (err) {
console.log('读取失败');
return
}
console.log(data);
}))
// 根据id获取
model.findById('001', (err, data) => {
if (err) {
console.log('失败');
return
}
console.log(data);
})
//批量获取
model.find({ name: '孙悟空' }, (err, data) => {
if (err) {
console.log('失败');
return
}
console.log('获取所有为孙悟空的数据', data);
})
model.find((err, data) => {
if (err) {
console.log('失败');
return
}
console.log('获取所有的数据', data);
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 条件控制
# 运算符
<=!== 等运算符,需要使用替代符号在 mongodb 不能><>=
- >使用$gt
- <使用$lt
- >=使用$gte
- <=使用$lte
- !== 使用**$ne**
db.students.find({id:{Sgt:3); id号比3大的所有的记录
1
# 逻辑运算
let model = mongoose.model('info', BookSchema)
// 获取 author 为孙悟空或猪八戒的数据
model.find({ $or: [{ author: '孙悟空' }, { author: '猪八戒' }] }, (err, data => {
}))
// 获取价格大于30小于70的数据
let model = mongoose.model('info', BookSchema)
model.find({ $and: [{ price: { $gt: 30 } }, { author: { $lt: 70 } }] }, (err, data => {
}))
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 正则匹配(模糊查询)
let model = mongoose.model('info', BookSchema)
model.find({ name: new RegExp('三') }, (err, data => {
}))
1
2
3
2
3
# 个性化读取
# 字段筛选
设置为0不返回 设置为1返回
// 设置只返回的字段 id默认返回
model.find().select({ name: 1, author: 1 }).exec((err, data) => {
if (err) {
console.log('查询失败');
return
}
console.log(data);
})
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 数据排序
升序为1 倒叙为-1
// model.find().select({name:1,price:1,_id:0}).sort({ price: 1 }).exec((err, data) => {
model.find().sort({ price: 1 }).exec((err, data) => {
if (err) {
console.log('查询失败');
return
}
console.log(data);
})
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 数据截取
- skip
- limit
model.find().skip(10).limit(10).exec((err, data) => {
if (err) {
console.log('查询失败');
return
}
console.log(data);
})
1
2
3
4
5
6
7
2
3
4
5
6
7
# 图形化工具
- Robo 3T免费
- Navicat 收费 有破解版(https://learnku.com/articles/67706)亲测有用
编辑 (opens new window)
上次更新: 2023/11/01, 22:15:03