MongoDB安裝及接入springboot的詳細過程
環(huán)境:windows、jdk8、springboot2
1.MongoDB概述
MongoDB是一個開源、高性能、無模式(模式自由)的文檔(Bson)型數(shù)據(jù)庫;其特點如下:
- 模式自由 ---- 不需要提前創(chuàng)建表 直接放數(shù)據(jù)就可以
- 支持高并發(fā) 2000以上
- 搭建集群比較方便
- 支持海量數(shù)據(jù)存儲
1.1 MongoDB基礎概念
1.1.1 MongoDB數(shù)據(jù)類型
數(shù)據(jù)類型 | 描述 |
---|---|
String | 字符串。存儲數(shù)據(jù)常用的數(shù)據(jù)類型。在 MongoDB 中,UTF-8 編碼的字符串才是合法的。 |
Integer | 整型數(shù)值。用于存儲數(shù)值。根據(jù)你所采用的服務器,可分為 32 位或 64 位。 |
Boolean | 布爾值。用于存儲布爾值(真/假)。 |
Double | 雙精度浮點值。用于存儲浮點值。 |
Min/Max keys | 將一個值與 BSON(二進制的 JSON)元素的最低值和最高值相對比。 |
Array | 用于將數(shù)組或列表或多個值存儲為一個鍵。 |
Timestamp | 時間戳。記錄文檔修改或添加的具體時間。 |
Object | 用于內(nèi)嵌文檔。 |
Null | 用于創(chuàng)建空值。 |
Symbol | 符號。該數(shù)據(jù)類型基本上等同于字符串類型,但不同的是,它一般用于采用特殊符號類型的語言。 |
Date | 日期時間。用 UNIX 時間格式來存儲當前日期或時間。你可以指定自己的日期時間:創(chuàng)建 Date 對象,傳入年月日信息。 |
Object ID | 對象 ID。用于創(chuàng)建文檔的 ID。 自動生成的一個主鍵 |
Binary Data | 二進制數(shù)據(jù)。用于存儲二進制數(shù)據(jù)。 |
Code | 代碼類型。用于在文檔中存儲 JavaScript 代碼。 |
Regular expression | 正則表達式類型。用于存儲正則表達式。 |
1.1.2 MongoDB基礎語法
// 查看數(shù)據(jù)庫 show dbs // 創(chuàng)建數(shù)據(jù)庫 // use 數(shù)據(jù)庫名稱 如果數(shù)據(jù)庫名稱存在,那么切換到該數(shù)據(jù)庫,不如不存在,那就新增數(shù)據(jù)庫 use commentdb // 查看當前所在數(shù)據(jù)庫 db // 刪除數(shù)據(jù)庫 // db.dropDatabase() 刪除當前所在的數(shù)據(jù)庫 db.dropDatabase() // 查看集合 show tables // 新增集合 // db.createCollection(集合名) db.createCollection("student") // 刪除集合 // db.集合名.drop() db.student.drop() // 新增文檔 // db.集合名.insert(文檔) json格式的文檔 db.comment.insert({"name":"tom","age":18}) db.comment.insert({"_id":2,"name":"jerry","age":19}) // 查看所有文檔 // db.集合名.find() db.comment.find() //查詢數(shù)據(jù) db.表名.find([query],[fields])
2. 安裝下載MongoDB
Download MongoDB Community Server | MongoDB
Download MongoDB Community Server non-relational database to take your next big project to a higher level!
https://www.mongodb.com/try/download/community
更改自己的安裝位置
取消勾選圖形化工具
如果需要外網(wǎng)連接MongoDB的話,需要改一下配置,bin 目錄下的 mongod.cfg
2.1 啟動MongoDB
在安裝目錄下創(chuàng)建啟動腳本:
mongod --dbpath D:\MongoDB
后面的地址為數(shù)據(jù)存儲地址
訪問地址:localhost:27017
同下方一樣則啟動成功
3.springboot集成MongoDB
3.1 導入依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
3.2 添加配置
spring: data: mongodb: host: xxx.xxx.xxx.xxx port: 27017 database: leadnews_comment
3.3 使用MongoDB
直接就可以在代碼中引入template調(diào)用方法就可以使用了
@Autowired private MongoTemplate mongoTemplate;
到此這篇關于MongoDB安裝及接入springboot的文章就介紹到這了,更多相關springboot MongoDB安裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!