这是一篇介绍mongoose的基本使用方法。

1. MongoDB数据库的基本概念

  • 数据库(可以有多个)
  • 一个数据库可有多个集合(collection 对应MySQL中的表)
  • 一个集合可以有多个文档(对应MySQL中的表记录)
  • 文档结构很灵活,没有任何限制
  • MongoDB很灵活,不需要像MySQL一样先创建数据库,表,设计表结构
    • 当你需要插入数据的时候,只需指定在哪个数据库哪个集合操作就可以了
    • 一切都有MongoDB自动完成建库建表这些操作
1
2
3
4
5
6
7
8
9
10
11
12
// 每一条数据都是一个对象
{
test: {
users: [
{},
{},
{},
...
]
},
cats: {},
}

2. mongoose学习

2.1 起步

2.1.1 安装mongoose库:

1
npm i mongoose

2.1.2 hello word

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const mongoose = require('mongoose');
//连接MongoDB数据库
mongoose.connect('mongodb://localhost/test');

//创建一个模型,就是设计数据库
//MongoDB是动态的,很灵活,在代码中设计数据库就行了
//mongoose这个包可以让你设计编写的过程非常简单
const Cat = mongoose.model('Cat', {name: String});

// //实例化一个cat
// const kitty = new Cat({name: 'Zildjian'});
// //持久化保持kitty实例
// kitty.save().then(() => console.log('meow'));

// 循环插入
for (let i = 0; i < 20; i++) {
const kitty = new Cat({name: 'mimi' + i});
kitty.save().then(() => console.log('meow'));
}

2.2 官方demo

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
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

//1. 连接数据库,连接的数据库不一定存在,当插入第一条数据后就会被自动创建出来
mongoose.connect('mongodb://localhost:/test');

//2. 设计文档结构(表结构)
// 字段名就是表结构中的属性名
//约束的目的是为了保证数据的完整性,不要有脏数据
let userSchema = new Schema({
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
email: {
type: String
}
});

// 3. 将文档发布为模型
// mongoose.model 方法就是用来将一个架构发布为 model
// 第一个参数:传入一个大写名词单数字符串用来表示你的数据库名称
// mongoose 会自动将大写名词的字符串生成 小写复数 的集合名称
// 例如这里的 User 最终会变为 users 集合名称
// 第二个参数:架构 Schema
// 返回值:模型构造函数
let user = mongoose.model('User', userSchema);

// 有了模型构造函数之后,就可以使用这个构造函数对users集合进行增删改查了。

2.2.1 添加数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 按照上面设计的Schema样式来写
let admin = new user({
username: 'tom',
password: '123456',
email: 'tom@admin.com'
});
// 保存数据
admin.save(function (err, res){
if(err){
console.log('保存失败');
}else{
console.log('保存成功');
}
});

2.2.2 查询数据

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
//查询数据,查不到返回 []
user.find(function (err, res){
if(err){
console.log('查询失败');
}else{
console.log(res); //结果是一个数组
}
});

//按条件查询,查不到返回 []
user.find({username: 'tom'}, function (err, res){
if(err){
console.log('查询失败');
}else{
console.log(res); //结果是一个数组
}
});

//有条件就按条件查找第一个,没有条件就查找第一个插入的数据,查不到返回null
user.findOne({
username: 'tom',
email: 'tom@admin.com'
},
function (err, res){
if(err){
console.log('查询失败');
}else{
console.log(res); //结果是一个对象
}
});

2.2.3 删除数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//删除数据
user.remove({
username: 'tom'
},
function (err, res){
if(err){
console.log('删除失败');
}else{
console.log('删除成功'); // 删除成功
console.log(res); // { n: 1, ok: 1 }
}
});

// 根据条件删除
Model.findOneAndRemove(conditions, [options], [callback])

// 根据id删除一个
Model.findIdAndAndRemove(id, [options], [callback])

2.2.4 更新数据

1
2
3
4
5
6
7
8
9
10
11
12
13
// 根据id更新一个数据
user.findByIdAndUpdate('5c1266fd3ac07d06b852e37f', {
password: '111111'
}, function (err, res){
if(err) return console.log('更新失败');
console.log('更新成功');
});

// 根据条件更新所有
Model.update(conditions, doc, [options], [callback])

// 根据指定条件更新一个
Model.findOneAndUpdate([conditions], [update], [options], [callback])