MongoDB快速入門及其SpringBoot實(shí)戰(zhàn)教程
MongoDB快速入門及其SpringBoot實(shí)戰(zhàn)
MongoDB簡介
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫。由 C++ 語言編寫。旨在為 WEB 應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
MongoDB是一個(gè)開源、高性能、無模式的文檔型數(shù)據(jù)庫,當(dāng)初的設(shè)計(jì)就是用于簡化開發(fā)和方便擴(kuò)展,是NoSQL數(shù)據(jù)庫產(chǎn)品中的一種。是最像關(guān)系型數(shù)據(jù)庫(MySQL)的非關(guān)系型數(shù)據(jù)庫。
它支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是一種類似于JSON的格式叫BSON,所以它既可以存儲(chǔ)比較復(fù)雜的數(shù)據(jù)類型,又相當(dāng)?shù)撵`活。
MongoDB概念解析
SQL術(shù)語/概念 | MongoDB術(shù)語/概念 | 解釋/說明 |
---|---|---|
database | database | 數(shù)據(jù)庫 |
table | collection | 數(shù)據(jù)庫表/集合 |
row | document | 數(shù)據(jù)記錄行/文檔 |
column | field | 數(shù)據(jù)字段/域 |
index | index | 索引 |
table joins | 表連接,MongoDB不支持 | |
primary key | primary key | 主鍵,MongoDB自動(dòng)將_id字段設(shè)置為主鍵 |
SQL與MongoDB數(shù)據(jù)存儲(chǔ)形式對(duì)比如下圖所示:
MongoDB數(shù)據(jù)類型
數(shù)據(jù)類型 | 描述 |
---|---|
String | 字符串。存儲(chǔ)數(shù)據(jù)常用的數(shù)據(jù)類型。在 MongoDB 中,UTF-8 編碼的字符串才是合法的。 |
Integer | 整型數(shù)值。用于存儲(chǔ)數(shù)值。根據(jù)你所采用的服務(wù)器,可分為 32 位或 64 位。 |
Boolean | 布爾值。用于存儲(chǔ)布爾值(真/假)。 |
Double | 雙精度浮點(diǎn)值。用于存儲(chǔ)浮點(diǎn)值。 |
Min/Max keys | 將一個(gè)值與 BSON(二進(jìn)制的 JSON)元素的最低值和最高值相對(duì)比。 |
Array | 用于將數(shù)組或列表或多個(gè)值存儲(chǔ)為一個(gè)鍵。 |
Timestamp | 時(shí)間戳。記錄文檔修改或添加的具體時(shí)間。 |
Object | 用于內(nèi)嵌文檔。 |
Null | 用于創(chuàng)建空值。 |
Symbol | 符號(hào)。該數(shù)據(jù)類型基本上等同于字符串類型,但不同的是,它一般用于采用特殊符號(hào)類型的語言。 |
Date | 日期時(shí)間。用 UNIX 時(shí)間格式來存儲(chǔ)當(dāng)前日期或時(shí)間。你可以指定自己的日期時(shí)間:創(chuàng)建 Date 對(duì)象,傳入年月日信息。 |
Object ID | 對(duì)象 ID。用于創(chuàng)建文檔的 ID。 |
Binary Data | 二進(jìn)制數(shù)據(jù)。用于存儲(chǔ)二進(jìn)制數(shù)據(jù)。 |
Code | 代碼類型。用于在文檔中存儲(chǔ) JavaScript 代碼。 |
Regular expression | 正則表達(dá)式類型。用于存儲(chǔ)正則表達(dá)式。 |
MongoDB特點(diǎn)
- 高性能:MongoDB提供高性能的數(shù)據(jù)持久性。特別是,對(duì)嵌入式數(shù)據(jù)模型的支持減少了數(shù)據(jù)庫系統(tǒng)上的I/O活動(dòng)。索引支持更快的查詢。
- 高可用性:MongoDB的復(fù)制工具稱為副本集(replica set),它可提供自動(dòng)故障轉(zhuǎn)移和數(shù)據(jù)冗余。
- 高擴(kuò)展性:MongoDB提供了水平可擴(kuò)展性作為其核心功能的一部分。分片將數(shù)據(jù)分布在一組集群的機(jī)器上。(海量數(shù)據(jù)存儲(chǔ),服務(wù)能力水平擴(kuò)展)
- 豐富的查詢支持:MongoDB支持豐富的查詢語言,支持讀和寫操作(CRUD),比如數(shù)據(jù)聚合、文本搜索和地理空間查詢等。
MongoDB下載與安裝
MongoDB下載網(wǎng)址:https://www.mongodb.com/try/download/community
圖形化界面MongoDB Compass下載網(wǎng)址: https://www.mongodb.com/try/download/compass
創(chuàng)建數(shù)據(jù)目錄
MongoDB 將數(shù)據(jù)目錄存儲(chǔ)在 db 目錄下。但是這個(gè)數(shù)據(jù)目錄不會(huì)主動(dòng)創(chuàng)建,我們?cè)诎惭b完成后需要?jiǎng)?chuàng)建它。
例如:在D盤創(chuàng)建一個(gè) data 的目錄,然后在 data 目錄里創(chuàng)建 db 目錄。
啟動(dòng)MongoDB
在MongoDB 目錄的 bin 目錄中執(zhí)行 mongod.exe 文件
D:\MongoDB\bin>mongod --dpath d:\data\db
MongoDB啟動(dòng)成功后,默認(rèn)端口是27017
Compass連接MongoDB
連接成功后界面如下:
SpringBoot實(shí)戰(zhàn)
功能需求
實(shí)現(xiàn)文章評(píng)論的增刪改查,參考示例如圖所示:
表結(jié)構(gòu)分析
數(shù)據(jù)庫:articledb
字段名稱 | 字段含義 | 字段類型 | 備注 |
---|---|---|---|
_id | ID | ObjectId或String | Mongo的主鍵的字段 |
articleid | 文章ID | String | |
content | 評(píng)論內(nèi)容 | String | |
userid | 評(píng)論人ID | String | |
nickname | 評(píng)論人昵稱 | String | |
createdatetime | 評(píng)論的日期時(shí)間 | Date | |
likenum | 點(diǎn)贊數(shù) | Int32 | |
replynum | 回復(fù)數(shù) | Int32 | |
state | 狀態(tài) | String | 0:不可見;1:可見; |
parentid | 上級(jí)ID | String | 如果為0表示文章的頂級(jí)評(píng)論 |
文章微服務(wù)模塊搭建
搭建項(xiàng)目工程article,項(xiàng)目目錄結(jié)構(gòu)如下
引入MongoDB依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
創(chuàng)建application.yml
注意,需先在MongonDB中創(chuàng)建articledb數(shù)據(jù)庫
spring: data: mongodb: host: 127.0.0.1 database: articledb port: 27017
創(chuàng)建啟動(dòng)類
@SpringBootApplication public class ArticleApplication { public static void main(String[] args) { SpringApplication.run(ArticleApplication.class, args); } }
啟動(dòng)項(xiàng)目,看能否正常運(yùn)行。
文章實(shí)體類的創(chuàng)建
@Data @Document(collection = "comment") // 指定為comment集合 @CompoundIndex(def = "{'userid':1}") // 在userid上建立升序索引 public class Comment implements Serializable { @Id private String id;//主鍵 //該屬性對(duì)應(yīng)mongodb的字段的名字,如果一致,則無需該注解 @Field("content") private String content;//評(píng)論內(nèi)容 private Date publishtime;//發(fā)布日期 //添加了一個(gè)單字段的索引 @Indexed private String userid;//發(fā)布人ID private String nickname;//昵稱 private LocalDateTime createdatetime;//評(píng)論的日期時(shí)間 private Integer likenum;//點(diǎn)贊數(shù) private Integer replynum;//回復(fù)數(shù) private String state;//狀態(tài) private String parentid;//上級(jí)ID private String articleid; }
文章評(píng)論持久層的創(chuàng)建
創(chuàng)建持久層時(shí),需繼承MongoRepository接口
public interface CommentRepository extends MongoRepository<Comment, String> { }
文章評(píng)論service層的創(chuàng)建
@Service public class CommentService { @Autowired private CommentRepository commentRepository; /** * 保存評(píng)論 * @param comment */ public void saveComment(Comment comment){ commentRepository.save(comment); } /** * 更新評(píng)論 * @param comment */ public void updateComment(Comment comment){ commentRepository.save(comment); } /** * 根據(jù)id刪除評(píng)論 * @param id */ public void deleteCommentById(String id){ commentRepository.deleteById(id); } /** * 查詢所有評(píng)論 * @return */ public List<Comment> findCommentList(){ return commentRepository.findAll(); } /** * 根據(jù)id查詢?cè)u(píng)論 * @param id * @return */ public Comment findCommentById(String id){ return commentRepository.findById(id).get(); } /** * 文章評(píng)論點(diǎn)贊,點(diǎn)贊數(shù)+1 * @param id */ public void updateCommentLikenum(String id){ Query query = new Query(Criteria.where("_id").is(id)); Update update = new Update(); update.inc("likenum"); mongoTemplate.updateFirst(query, update, Comment.class); } }
文章評(píng)論微服務(wù)測試
@SpringBootTest(classes = ArticleApplication.class) @RunWith(SpringRunner.class) public class CommentServiceTest { @Autowired private CommentService commentService; @Test public void testFindComment(){ List<Comment> commentList = commentService.findCommentList(); System.out.println(commentList); } @Test public void testFindCommentById(){ Comment comment = commentService.findCommentById("1"); System.out.println(comment); } @Test public void testSaveComment(){ Comment comment = new Comment(); comment.setArticleid("100002"); comment.setContent("樊神yyds"); comment.setCreatedatetime(LocalDateTime.now()); comment.setUserid("1003"); comment.setNickname("隨緣夏沫"); comment.setState("1"); comment.setLikenum(0); comment.setReplynum(0); commentService.saveComment(comment); } @Test public void testFindCommentListByParentid(){ Page<Comment> page = commentService.findCommentListByParentid("1", 1, 2); System.out.println(page.getContent()); } @Test public void testUpdateCommentLikenum(){ commentService.updateCommentLikenum("2"); } }
到此這篇關(guān)于MongoDB快速入門及其SpringBoot實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)MongoDB入門內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB入門教程之常用的運(yùn)維技術(shù)介紹
這篇文章主要介紹了MongoDB入門教程之常用的運(yùn)維技術(shù)介紹,講解了安裝部署、狀態(tài)監(jiān)控、安全認(rèn)證、備份和恢復(fù)等內(nèi)容,需要的朋友可以參考下2014-08-08MongoDB的Master-Slave主從模式配置及主從復(fù)制要點(diǎn)解析
主從復(fù)制是數(shù)據(jù)庫運(yùn)維中一種常見的備份方式,這里我們來看一下MongoDB的Master-Slave主從模式配置及主從復(fù)制要點(diǎn)解析,需要的朋友可以參考下2016-06-06MongoDB數(shù)據(jù)庫類replace替換字符串指定內(nèi)容
mongoDB是沒有定義replace函數(shù)的,那么如果有需求需要替換nongo中數(shù)據(jù)的某一部分,怎么辦?下面這篇文章主要給大家介紹了關(guān)于MongoDB數(shù)據(jù)庫類replace替換字符串指定內(nèi)容的相關(guān)資料,需要的朋友可以參考下2023-05-05MongoDB用Mongoose得到的對(duì)象不能增加屬性完美解決方法(兩種)
本文給大家分享兩種解決方案解決MongoDB用Mongoose得到的對(duì)象不能增加屬性問題,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2017-11-11MongoDB系列教程(四):設(shè)置用戶訪問權(quán)限
這篇文章主要介紹了MongoDB系列教程(四):設(shè)置用戶訪問權(quán)限,本文講解了在Windows環(huán)境下如何創(chuàng)建用戶認(rèn)證,需要的朋友可以參考下2015-05-05