MongoDB
1. 什么是MongoDB
分布式文件存儲數(shù)據(jù)庫,屬于NoSQL之一
基于BSON格式做數(shù)據(jù)存儲和處理
適合數(shù)據(jù)項多變的記錄,存儲結(jié)構(gòu)靈活
便于擴(kuò)展和使用,支持索引橄浓、查詢、事務(wù)(最新版)
支持Java亮航、PHP贮配、Python等多種語言
2. 啟動MongoDB
2.1 啟動相關(guān)服務(wù)
mongod --dbpath C:\mongodb\db --logpath C:\mongodb\log\4.28.log --port 27017
驗證服務(wù)是否啟動
It looks like you are trying to access MongoDB over HTTP on the native driver port.
服務(wù)啟動成功
2.2 啟動客戶端
在添加了Path路徑之后在cmd中輸入
mongo
3. DB相關(guān)命令
3.1 顯示所有數(shù)據(jù)庫信息
> show dbs;
admin 0.000GB
config 0.000GB
flashexpresslog 0.000GB
local 0.000GB
3.2 切換數(shù)據(jù)庫
使用Use命令, 可以切換到已存在的數(shù)據(jù)庫, 也可以切換到不存在的數(shù)據(jù)庫, 若切換到不存在的數(shù)據(jù)庫, 這個數(shù)據(jù)庫在內(nèi)存中將要被創(chuàng)建
> use admin
switched to db admin
3.3 查看當(dāng)前所在的數(shù)據(jù)庫
> db
admin
3.4 創(chuàng)建數(shù)據(jù)庫
MongoDB是弱類型的, 需要在DB中添加集合或文檔, 然后數(shù)據(jù)庫會自動創(chuàng)建
3.4.1 通過常見集合創(chuàng)建數(shù)據(jù)庫
> use test
switched to db test
> db.createCollection('collection1')
{ "ok" : 1 }
3.4.2 通過創(chuàng)建文檔來創(chuàng)建數(shù)據(jù)庫
沒有數(shù)據(jù)庫則創(chuàng)建數(shù)據(jù)庫, 沒有collection1集合則創(chuàng)建該集合, 在該集合中插入一個空文檔
> db.collection1.insert({})
WriteResult({ "nInserted" : 1 })
3.5 刪除數(shù)據(jù)庫
先進(jìn)入要闡述的數(shù)據(jù)庫, 然后執(zhí)行對應(yīng)的刪除方法
> db.dropDatabase();
{ "dropped" : "test", "ok" : 1 }
3.6 查看幫助命令
> help
3.7 退出
> exit
4. Collection相關(guān)命令
4.1 查看DB中的集合
> show collections;
4.2 創(chuàng)建集合
方式一: 創(chuàng)建一個空集合
> db.createCollection("collect1");
{ "ok" : 1 }
方式二: 插入一個新文檔
> db.collection.insert({});
WriteResult({ "nInserted" : 1 })
4.3 刪除集合
> db.collection.drop();
true
注意
所有的命令, 都是以db開頭的
5. Document相關(guān)命令
相當(dāng)于MySQL中的DML語句: insert, update, delete, dql語句:select
5.1 增加相關(guān)
在插入文檔時, 如果沒有指定id, 系統(tǒng)會自動為我們添加一個主鍵, 這個主鍵是ObjectId類型的一個隨機(jī)數(shù)
> db.user.insert({uname:"mao1",age:18});
WriteResult({ "nInserted" : 1 })
> db.user.find();
{ "_id" : ObjectId("5eacc99385c16b1758c67b68"), "uname" : "mao1", "age" : 18 }
指定Id, 插入一條文檔
> db.user.insert({_id:1,uname:"mao2",age:28});
WriteResult({ "nInserted" : 1 })
> db.user.find();
{ "_id" : ObjectId("5eacc99385c16b1758c67b68"), "uname" : "mao1", "age" : 18 }
{ "_id" : 1, "uname" : "mao2", "age" : 28 }
插入多條數(shù)據(jù)就是一個數(shù)組
Save命令
也是作為插入使用, 但是跨界, 若指定了ID, 若該ID存在, 則更新該條數(shù)據(jù), 否則創(chuàng)建該數(shù)據(jù)
> db.user.save({_id:13,uname:"mao13",age:38});
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 13 })
> db.user.save({_id:13,uname:"mao23",age:18});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
5.2 更新相關(guān)
待續(xù)
5.3 刪除相關(guān)
待續(xù)
5.4 查詢相關(guān)
待續(xù)
6. SpringBoot使用MongoDB
5.1 引入Jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
5.2 配置參數(shù)
#mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=flashexpresslog
5.3 使用MongoDB
@Autowired
private MongoTemplate mongo;
@Override
public void log(int userId, String ip) {
//記錄用戶登錄日志信息
LoginHistory loginHistory = new LoginHistory();
loginHistory.setUserId(userId);
loginHistory.setLoginTime(new Date());
loginHistory.setIp(ip);
mongo.save(loginHistory);
}
}
待更