MongoDB 是一个流行的开源文档型数据库,使用类似 JSON 的 BSON 文档模型存储数据,由 MongoDB Inc. 开发。它旨在为 Web 应用提供可扩展的高性能数据存储方案,是介于关系型与非关系型数据库之间、功能最丰富且最像关系型数据库的产品。
其核心思想是 以灵活的文档模型替代固定的表结构,从而让海量、半结构化数据的存储与横向扩展更加自然高效。
1. 概述
MongoDB 是一个流行的开源文档型数据库,采用类似 JSON 的 BSON 文档模型存储数据,由 MongoDB Inc. 开发,旨在为 Web 应用提供可扩展的高性能数据存储方案。它用灵活的文档模型替代固定的表结构,并以副本集与分片机制实现横向扩展,特别适合高并发、海量数据和高可用的「三高」业务场景。
2. 业务应用场景
- 高并发:对数据库高并发读写的需求
- 海量数据:对海量数据的高效率存储和访问的需求
- 高可扩展 / 高可用:对数据库的高可扩展性和高可用性的需求
3. MongoDB 基础
MongoDB 是 NoSQL 数据库,它的记录是一个文档,由字段和值对(field-value pairs)组成。
2.1 数据类型
| 类型 | 整数 | 别名 | 备注 | 说明 |
|---|---|---|---|---|
| Double | 1 | double | shell 中的数字类型 | 64 位浮点数 |
| String | 2 | string | 字符串类型 | |
| Object | 3 | object | 对象类型 | |
| Array | 4 | array | 数组类型 | |
| Binary data | 5 | binData | shell 中不可用 | 二进制数据类型 |
| Undefined | 6 | undefined | 已过时 | 未定义类型 |
| ObjectId | 7 | objectId | 对象 id 类型 | |
| Boolean | 8 | bool | 布尔类型 | |
| Date | 9 | date | 日期类型 | |
| Null | 10 | null | 用于表示空值或者不存在的字段 | |
| Regular Expression | 11 | regex | 正则表达式类型 | |
| DBPointer | 12 | dbPointer | 已过时 | |
| JavaScript | 13 | javascript | JavaScript 代码 | |
| Symbol | 14 | symbol | shell 中不可用,已过时 | |
| JavaScript(with scope) | 15 | javascriptWithScope | 带作用域的 JavaScript 代码 | |
| 32-bit integer | 16 | int | shell 中不可用 | 32 位整数 |
| Timestamp | 17 | timestamp | 时间戳类型 | |
| 64-bit integer | 18 | long | shell 中不可用 | 64 位整数 |
| Decimal128 | 19 | decimal | 3.4 版本新增 | |
| Min key | -1 | minKey | shell 中无此类型 | 最小键 |
| Max key | 127 | maxKey | shell 中无此类型 | 最大键 |
2.2 使用 MongoDB
2.2.1 Windows
下载地址:https://www.mongodb.com/try/download/community
命令行参数启动:
mongod --dbpath=..\data\db
配置文件启动:新建 config 文件夹与 conf,在 conf 文件夹创建 mongod.conf 文件。
storage:
dbPath:D:\mongodb-win32-x86_64-windows-8.0.0\data\db
storage:
dbPath: D:\mongodb-win32-x86_64-windows-8.0.0\data\db
port: 27017
bindIp: 127.0.0.1
2.2.2 Linux
2.2.2.1 上传 MongoDB 安装包
将从官网下载好的 mongodb-linux-x86_64-4.0.0.tgz 上传到要安装的服务器目录中,建议目录为:/data/lib/mongodb。
2.2.2.2 解压 MongoDB 安装包
cd /data/lib/mongodb
tar -zxvf mongodb-linux-x86_64-4.0.0.tgz
2.2.2.3 创建 MongoDB 必要目录
在 /usr/local 目录中创建 mongodb 文件夹:
cd /usr/local
mkdir mongodb
在 mongodb 下创建 data 和 logs 目录:
mkdir data
mkdir logs
touch /usr/local/mongodb/logs/mongodb.log
2.2.2.4 移动 MongoDB 安装目录
将解压后的 mongodb-linux-x86_64-4.0.0 中的文件全部移动到 /usr/local/mongodb 中:
mv mongodb-linux-x86_64-4.0.0/* /usr/local/mongodb
2.2.2.5 设置 MongoDB 环境变量
打开环境变量文件:
vim /etc/profile
在文件中增加以下配置:
export MONGODB_HOME=/usr/local/mongodb
export PATH=$MONGODB_HOME/bin:$PATH
保存后退出,使新增的环境变量立即生效:
source /etc/profile
2.2.2.6 添加 MongoDB 配置文件
vim /etc/mongodb.conf
添加以下常用配置:
#指定数据库路径
dbpath=/usr/local/mongodb/data
#指定MongoDB日志文件
logpath=/usr/local/mongodb/logs/mongodb.log
# 使用追加的方式写日志
logappend=true
#端口号
port=27017
#方便外网访问
bind_ip=0.0.0.0
fork=true # 以守护进程的方式运行MongoDB,创建服务器进程
#auth=true #启用用户验证
#bind_ip=0.0.0.0 #绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定则默认本地所有IP
2.2.2.7 启动和关闭 MongoDB
进入 bin 目录:
cd /usr/local/mongodb/bin
启动 MongoDB(使用配置文件方式启动):
mongod -f /etc/mongodb.conf
关闭 MongoDB(使用配置文件方式关闭):
mongod --shutdown -f /etc/mongodb.conf
2.2.2.8 创建 MongoDB 数据库及管理员账号
进入 mongodb 的命令行模式:
cd /usr/local/mongodb/bin
./mongo
切换到 admin 数据库,并创建 root(管理员)用户:
use admin;
db.createUser({user:'root', pwd:'123456', mechanisms : ["SCRAM-SHA-1"] ,roles:[{role:'root', db:'admin'}]});
创建好账号后,可以选择打开 mongodb.conf 中的 auth=true 授权验证。验证账号是否授权成功:
db.auth("root","123456")
如果返回 1 表示验证成功,如果是 0 表示验证失败。
2.3 数据库的创建与删除
2.3.1 创建
use articledb // 数据库名
db // 展示当前使用的数据库
MongoDB 默认的数据库:
admin 权限和用户
local 这个数据永远不会复制
config 分片设置
2.3.2 删除
db.dropDatabase()
2.4 集合的创建与删除
集合相当于传统关系型数据库中的「表」。
db.createCollection(name) // 显示创建
删除集合:
db.集合名.drop()
2.5 文档的增删改查
插入新文档时,_id 字段会自动生成:
db.collection.insert({
"articleId":1,"content":"good","userId":1001,"createTime":new Date()
})
查询文档:
db.collection.find()
//条件查询
db.collection.find({"userId":1002})
投影查询(0 为不显示,1 为显示):
db.collection.find({},{"content":1,_id:0})
2.6 通过 try catch 捕获错误
try{
db.collection.insertMany({
})
}catch(e){
print(e)
}
2.7 文档的更新
db.collection.update({"_id":"1"},{"name":"richu"})
db.collection.update({userId:"1001"}, {$set:{content:"test"}})
//批量更新
db.collection.update({_id:"67121a9cb63f7427aa0306b5"}, {$set:{userId:"test"}},{multi:true})
2.8 删除
db.collection.remove({"_id":"1"})
db.collection.remove({"_id":1}, options)
2.9 比较操作符
| 操作符 | 描述 | 示例 |
|---|---|---|
$eq |
等于 | { age: { $eq: 25 } } |
$ne |
不等于 | { age: { $ne: 25 } } |
$gt |
大于 | { age: { $gt: 25 } } |
$gte |
大于等于 | { age: { $gte: 25 } } |
$lt |
小于 | { age: { $lt: 25 } } |
$lte |
小于等于 | { age: { $lte: 25 } } |
$in |
在指定的数组中 | { age: { $in: [25, 30, 35] } } |
$nin |
不在指定的数组中 | { age: { $nin: [25, 30, 35] } } |
查找年龄大于 25 且城市为 “New York” 的文档:
db.myCollection.find({ age: { $gt: 25 }, city: "New York" });
2.10 逻辑操作符
| 操作符 | 描述 | 示例 |
|---|---|---|
$and |
逻辑与,符合所有条件 | { $and: [ { age: { $gt: 25 } }, { city: "New York" } ] } |
$or |
逻辑或,符合任意条件 | { $or: [ { age: { $lt: 25 } }, { city: "New York" } ] } |
$not |
取反,不符合条件 | { age: { $not: { $gt: 25 } } } |
$nor |
逻辑与非,均不符合条件 | { $nor: [ { age: { $gt: 25 } }, { city: "New York" } ] } |
2.11 元素操作符
| 操作符 | 描述 | 示例 |
|---|---|---|
$exists |
字段是否存在 | { age: { $exists: true } } |
$type |
字段的 BSON 类型 | { age: { $type: "int" } } |
查找包含 age 字段的文档:
db.myCollection.find({ age: { $exists: true } });
2.12 数组操作符
| 操作符 | 描述 | 示例 |
|---|---|---|
$all |
数组包含所有指定的元素 | { tags: { $all: ["red", "blue"] } } |
$elemMatch |
数组中的元素匹配指定条件 | { results: { $elemMatch: { score: { $gt: 80, $lt: 85 } } } } |
$size |
数组的长度等于指定值 | { tags: { $size: 3 } } |
查找数组 tags 中包含 “red” 和 “blue” 的文档:
db.myCollection.find({ tags: { $all: ["red", "blue"] } });
2.13 其他操作符
| 操作符 | 描述 | 示例 |
|---|---|---|
$regex |
匹配正则表达式 | { name: { $regex: /^A/ } } |
$text |
进行文本搜索 | { $text: { $search: "coffee" } } |
$where |
使用 JavaScript 表达式进行条件过滤 | { $where: "this.age > 25" } |
查找名字以 “A” 开头的文档:
db.myCollection.find({ name: { $regex: /^A/ } });
4. MongoDB 进阶
3.1 索引的创建
索引通常能够极大地提高查询的效率。如果没有索引,MongoDB 在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录。这种扫描全集合的查询效率非常低,特别在处理大量数据时,查询可能要花费几十秒甚至几分钟,这对网站的性能是非常致命的。索引是特殊的数据结构,存储在一个易于遍历读取的数据集合中,是对数据库表中一列或多列的值进行排序的一种结构。
在 MongoDB 中,常见的索引类型包括:
- 单字段索引:基于单个字段的索引。
- 复合索引:基于多个字段组合的索引。
- 文本索引:用于支持全文搜索。
- 地理空间索引:用于地理空间数据的查询。
- 哈希索引:用于对字段值进行哈希处理的索引。
createIndex() 方法基本语法格式如下所示:
db.collection.createIndex( keys, options )
db:数据库的引用。collection:集合的名称。keys:一个对象,指定了字段名和索引的排序方向(1 表示升序,-1 表示降序)。options:一个可选参数,可以包含索引的额外选项。
options 参数是一个对象,可以包含多种配置选项,以下是一些常用的选项:
unique:如果设置为true,则创建唯一索引,确保索引字段的值在集合中是唯一的。background:如果设置为true,则索引创建过程在后台运行,不影响其他数据库操作。name:指定索引的名称,如果不指定,MongoDB 会根据索引的字段自动生成一个名称。sparse:如果设置为true,创建稀疏索引,只索引那些包含索引字段的文档。expireAfterSeconds:设置索引字段的过期时间,MongoDB 将自动删除过期的文档。weights:为文本索引指定权重。
db.articledb.createIndex({userId:1})
3.2 创建哈希索引
从 MongoDB 3.2 版本开始,可以使用哈希索引对字段进行哈希,以支持大范围的数值查找。
db.collection.createIndex( { field: "hashed" } )
3.3 查看索引
使用 getIndexes() 方法可以查看集合中的所有索引:
db.collection.getIndexes()
3.4 删除索引
使用 dropIndex() 或 dropIndexes() 方法可以删除索引:
// 删除指定的索引
db.collection.dropIndex( "indexName" )
// 删除所有索引
db.collection.dropIndexes()
5. 业务场景的使用
4.1 基本业务
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<!-- MongoDB Driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
</dependency>
配置连接:
spring:
data:
mongodb:
database: mydb
host: 192.168.3.11
port: 27017
username: root
password: 123456
authentication-database: admin
实体类:
@Data //lombok 简化开发 不需要写set get
@Document(collection = "comment") //指定文档的名字
@CompoundIndex(def = "{'userid':1,'nickname':-1}") //指定索引 与MongoDB原生写法类似
public class Article implements Serializable {
private String id;
@Field("content") //给字段起别名
private String content;
private Date publishtime;
@Indexed //指明该字段为索引
private String userid;
private String nickname;
private Integer likenum;
private LocalDateTime createTime;
private Integer replynum;
private String state;
private String parentid;
private String articleid;
}
org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 2 (BadValue): 'Field 'locale' is invalid in: { locale: "comment" }' on server 127.0.0.1:27017. The full response is {"ok": 0.0, "errmsg": "Field 'locale' is invalid in: { locale: \"comment\" }", "code": 2, "codeName": "BadValue"}
Service 类:
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentReposity commentReposity;
@Override
public void Insert(Article comment) {
commentReposity.insert(comment);
}
@Override
public Article find(Integer Id) {
Optional<Article> article = commentReposity.findById(String.valueOf(Id));
Article article1 = article.get();
return article1;
}
}
进行测试:
@Test
public void testFindById(){
Article article = commentService.find(1);
System.out.println(article);
}
4.2 分页查询业务
CommentReposity:
public interface CommentReposity extends MongoRepository<Article, String> {
Page<Article> findByParentid(String author, Pageable pageable);
}
Service:
//分页查询
@Override
public Page<Article> findByPage(String parentId, Integer pageNo,Integer pageSize) {
return commentReposity.findByParentid(parentId, PageRequest.of(pageNo-1, pageSize));
}
4.3 MongoTemplate
@Autowired
private MongoTemplate mongoTemplate;
public void updateCommentLikeNum(String Id) {
//查询条件
Query query = new Query(Criteria.where("_id").is(Id));
//更新条件
Update update = new Update();
update.inc("likenum", 1);
mongoTemplate.updateFirst(query,update,Article.class);
}
添加:
User save = mongoTemplate.save(user, collectionName);
删除:
String collectionName = "springboot";
Criteria idCriteria = Criteria.where("id").is(666);
DeleteResult deleteResult = mongoTemplate.remove(new Query(idCriteria), User.class, collectionName);
6. MongoDB 高级
5.1 副本集
副本集由以下节点类型组成:
- 主节点(Primary)
- 次要 / 辅助 / 从节点(Secondary)
5.2 副本集的创建
初始化副本集配置:
rs.initiate(configuration)
添加副本节点与仲裁节点:
rs.add("192.168.3.133:27018") // 添加副本节点
rs.add("192.168.3.133:27019") // 添加仲裁节点
rs.slaveOk() // 在从节点设置自己作为从节点
一句话总结:MongoDB 以灵活的文档(BSON)模型与水平扩展能力,有效应对高并发、海量数据和高可用的「三高」业务需求。