MongoDb基礎(chǔ)知識點整理
- 可視化圖形界面 compass
- 應(yīng)用場景
- 針對三高請求
- High performance 對數(shù)據(jù)庫高并發(fā)的讀寫需求
- High Storage 高效率的訪問和讀取
- 對數(shù)據(jù)庫的高擴展性和高可用性
- 具體應(yīng)用場景
- 社交場景。利用MongoDB存儲用戶信息,以及用戶發(fā)表的朋友圈
- 游戲場景
- 物流場景
- 物聯(lián)網(wǎng)場景
- 視頻直播
- 針對三高請求
- MongoDB術(shù)語/概念
- database 數(shù)據(jù)庫
- collection 數(shù)據(jù)庫表/集合
- document 數(shù)據(jù)記錄行/文檔
- field 數(shù)據(jù)字段/域
- index 索引
- 嵌入文檔 MongoDB通過嵌入式文檔來替代多表連接
- primary key 主鍵MongoDB自動將_id 字段設(shè)置為主鍵
MongoDb linux 安裝
- 下載linux安裝包(ps:MongoDB的版本號命名規(guī)則:大版本號.中版本號.小版本號,當中版本號為奇數(shù)時,指的是開發(fā)版本,當中版本號為偶數(shù)時,指的是穩(wěn)定版本,所以盡量找奇數(shù)版本下載)
- 解壓
tar zxvf mongodb-linux-x86_64-rhel70-4.2.5.tgz - 移動目錄
mv mongodb-linux-x86_64-rhel70-4.2.5 /usr/local/mongodb - 創(chuàng)建數(shù)據(jù)存儲目錄 和 日志目錄
mkdir -p /mongodb/single/data/db mkdir -p /mongodb/single/logs - 創(chuàng)建并修改配置文件
vim /mongodb/single/mongodb.conf- 配置文件如下
systemLog: destination: file path: "/mongodb/single/logs/mongod.log" logAppend: true storage: # 啟用或禁用持久性日志,保證可恢復 dbPath: "/mongodb/single/data/db" journal: enabled: true processManagement: # 啟用后臺守護進程 fork: true net: # 第二個指的是局域網(wǎng) bindIp: 127.0.0.1,172.16.146.100 port: 27017 setParameter: enableLocalhostAuthBypass: false - 啟動服務(wù)
/usr/local/mongodb/bin/mongod -f /mongodb/single/mongodb.conf - 注意事項
- 校驗遠程連接,如果遠程連接未能相應(yīng)連接,請檢查防火墻,具體命令如下
如果是running狀態(tài),請關(guān)閉防火墻,或者開放特定端口systemctl status firewalld
關(guān)閉防火墻的命令如下systemctl stop firewalld- 服務(wù)器器是阿里云的,請注意開放阿里云自己的安全策略
mongoDb常用的操作命令
- 數(shù)據(jù)庫操作
- 數(shù)據(jù)庫展示
show dbs - 數(shù)據(jù)庫創(chuàng)建或者轉(zhuǎn)換
use test - 展示當前數(shù)據(jù)庫
db - 刪除數(shù)據(jù)庫
db.dropDatabase()
- 數(shù)據(jù)庫展示
- 集合操作
- 創(chuàng)建集合
db.createCollection("test") - 刪除集合
db.test.drop() - 展示集合
show collections
- 創(chuàng)建集合
- 文檔基本的CRUD
- 插入
db.test.insert(Bson) db.test.insertMany(BSon集合) # 批量插入的時候使用try catch try { db.test.insertMany(BSon集合); }catch(e){ print(e) }- 刪除
# 刪除全部數(shù)據(jù) db.test.remove({}); # 刪除部分數(shù)據(jù) db.test.remove();- 更新
db.test.update(query,update,options); #覆蓋修改 會把之前的所有數(shù)據(jù)都覆蓋 db.test.update({_id:"1"},{name:"111"}); #局部修改,只修改部分數(shù)據(jù) db.test.update({_id:"1"},{$set:{name:"111"}}); #批量修改 不加參數(shù)只會修改第一條 db.test.update({userId:"1"},{$set:{name:"111"}},{multi:true}); #自增長 db.test.update({userId:"1"},{$inc:{name:NumberInt(1)}});- 查詢
db.test.find() db.test.find({}) db.test.findOne({}) db.test.find({name:"123"},{name:1})