
🍁博客主页:👉不会压弯的小飞侠 ?欢迎关注:👉点赞👍收藏?留言? ?系列专栏:👉SpringBoot专栏(每日更新) ?如果觉得博主的文章还不错的话,请三连支持一下博主。 🔥欢迎大佬指正,一起 学习!一起加油! 

🍁MongoDB简介
-
MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统;在高负载的情况下,添加更多的节点,可以保证服务器性能;MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案;MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成;MongoDB 文档类似于 JSON 对象。 -
MongoDB是一个开源、高性能、无模式的文档型数据库。NoSQL数据库产品中的一种,是最像关系型数据库的非关系型数据库。
🔥应用场景
- 游戏装备数据、游戏道具数据
- 存储位置:数据库、Mongodb
- 特征:永久性存储与临时存储相结合、修改频度较高
- 直播数据、打赏数据、粉丝数据
- 存储位置:数据库、Mongodb
- 特征:永久性存储与临时存储相结合,修改频度极高
- 物联网数据
- 存储位置:Mongodb
- 特征:临时存储,修改频度飞速
🍁MongoDB下载与安装
- windows版Mongo下载👉 点击下载
- Windows版Mongo安装
- 解压缩后设置数据目录
- 在bin文件下创建一个data包,打开data再创建一个db包


🍁开启MongoDB服务
- 输入命令:
mongod --dbpath=..\data\db



🍁Robo 3T下载安装
- Robo 3T下载👉点击下载
- 解压到文件夹里即可,这里就不过描述。
- 点击
robot3t.exe



- 点击
test 测试一下–>close –>save 即可

- 点击
connection 
🔥创建数据库和表
1.右键local –>create DataBase
 2.数据库名为study  3.点击Collections –>Create Collection...  4.表名为book 
🍁Mongodb基本操作
🔥添加数据
db.book.save({"name":"rc"})
db.book.save({"name":"rc",age:"22"})

🔥查看数据
db.getCollection('book').find({})

db.book.find({age:"22"})

🔥修改数据
db.book.update({name:"rc"},{$set:{name:"rcrc"}})

🔥删除数据
db.book.remove({age:"22"})

db.book.remove({})
🍁SpringBoot整合Mongodb
- 🔥 新建一个Springboot项目,这里就不过多描述,专栏里都有详细的教程。
???注意:导入相关依赖,勾上之后,springboot会自动导入所需的依赖。  Mongodb依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
🔥编写yml文件的配置
spring:
data:
mongdb:
url: mongdb://localhost/study
🔥客户端读写Mongdb
package com.jkj;
import com.jkj.domain.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import java.util.List;
@SpringBootTest
class Springboot11MongdbApplicationTests {
@Autowired
private MongoTemplate mongoTemplate;
@Test
void save() {
Book book=new Book();
book.setId(1);
book.setType("sing");
book.setName("sing");
book.setDescription("sing");
mongoTemplate.save(book);
}
@Test
void findAll(){
List<Book> all = mongoTemplate.findAll(Book.class);
System.out.println(all);
}
}
运行结果: 
|