NoSQL——MongoDB副本集和文檔管理
一. 副本集
1. 介紹
- 副本集也稱為MongoDB複製
- 在多個伺服器上存儲數據,實現數據同步
- 提高數據可用性、安全性,方便數據故障恢復
2. MongoDB複製原理
- 至少需要兩個節點,其中一個是主節點,負責處理客戶端請求,其餘是從節點,負責複製主節點數據。
- 常見搭配:一主一從,一主多從
- 主節點記錄所有操作,從節點定期輪詢主節點獲取這些操作,對自己的數據副本做操作,保持一致。
3. 副本集實現方式
(1) 主從複製方式(宕機後需要手動配置,已不用)
(2) Replica Sets副本集
- 1.6版本後支持,由於主從複製方式
- 故障自動切換,自動修復成員節點,降低運維成本
- 類似於高可用集群
4. 配置Replica Sets
(1) 環境準備,3台主機
- 192.168.4.51: 27051 主
- 192.168.4.52: 27052 從
- 192.168.4.53: 27053 從
(2) 每台主機安裝MongoDB同時配置文件加上一行:replSet rs1
vim /usr/local/mongodb/etc/mongodb.conf
logpath=/usr/local/mongodb/log/mongodb.log
logappend=true
dbpath=/usr/local/mongodb/data/db
fork=true
port=27051
bind_ip=192.168.4.51
replSet=rs1 #定義副本集名稱為rs1
(3) 配置節點信息
任意一台主機上執行如下操作:其中,config僅為名稱,可以自由定義。
config={
_id:"rs1",
members:[
{_id:0,host:"192.168.4.51:27051"},
{_id:1,host:"192.168.4.52:27052"},
{_id:2,host:"192.168.4.53:27053"}
]
}
(4) 初始化Replica Set環境
rs.initiate(config)
(5) 查看副本信息
- 查看狀態信息: rs.status()
- 查看是否為master庫: rs.isMaster()
- 同步數據驗證,允許從庫查看數據(從庫默認只同步數據,不能操作數據): db.getMongo().setSlaveOk()
二. 文檔管理
1. 插入文檔 save insert insertMany
- save: _id存在,則修改文檔欄位值,_id不存在,插入文檔
- insert: _id存在,放棄插入,_id不存在,插入文檔
#插入多行時,insert和insertMany都可以
db.aaa.insert([{name:"aaa",home:"beijing"},{name:"bbb",home:"shenzhen"}])
db.aaa.insertMany([{name:"newa",home:"beijing"},{name:"newb",home:"shenzhen"}])
2. 查詢文檔 fine findOne
- 行數顯示限制:
- limit(數字) //顯示前幾行
- skip(數字) //跳過前幾行
- sort({欄位名}) //1升序,-1降序
db.aaa.find();
db.passwd.find({shell:"/sbin/nologin"},{_id:0,name:1,uid:1}).limit(5) #顯示前5行
db.passwd.find({shell:"/sbin/nologin"},{_id:0,name:1,uid:1}).skip(2).limit(5) #跳過前2行,再顯示5行
db.passwd.find({shell:"/sbin/nologin"},{_id:0,name:1,uid:1}).sort({uid:1}) #uid升序排列
db.passwd.find({shell:"/sbin/nologin"},{_id:0,name:1,uid:1}).sort({uid:-1}) #uid降序排列
- 範圍比較:
- 相等 {欄位名:值}
- 邏輯與 {欄位名:值,欄位名:值}
- 邏輯或 在....里 {欄位名:{$in:[值1,值2,........值n]}}
- 邏輯或 不在....里 {欄位名:{$nin:[值1,值2,........值n]}}
- 邏輯或 或者 {$or:[{欄位名:值},{欄位名:值},{欄位名:值}.....]}
db.passwd.find({name:"root",uid:0},{_id:0,name:1,uid:1}) #邏輯與,滿足兩個條件
db.passwd.find({name:{$in:["mail","root","xxx"]}},{name:1,_id:0}) #邏輯或,三個條件滿足幾個顯示幾個
db.passwd.find({name:{$nin:["root","xxx"]}},{name:1,_id:0}) #邏輯或
db.passwd.find({$or:[{uid:1},{uid:8},{name:"root"}]},{name:1,uid:1,_id:0})
- 正則匹配
db.passwd.find({name:/^a/ },{_id:0,name:1,uid:1}) #匹配名字a開頭
db.passwd.find({name:/p$/ },{_id:0,name:1,uid:1}) #匹配名字p結尾
- 數值比較
- $lt(<) $lte(<=) $gt(>) $gte(>=) $ne(!=)
db.passwd.find({ uid:{$lt:5} },{_id:0,name:1,uid:1}) #uid<5
db.passwd.find({ uid:{$gt:1000} },{_id:0,name:1,uid:1}) #uid>1000
db.passwd.find({ uid:{$gte:100,$lte:200} },{_id:0,name:1,uid:1}) #100<=uid號<=200
db.passwd.find({ uid:{$gte:0,$lte:5,$ne:3} },{_id:0,name:1,uid:1}) #0<=uid號<=5且!=3
- null匹配
db.passwd.save({name:null,uid:null})
db.passwd.find({name:null})
3. 更新文檔
(1) update({條件},{修改的欄位})
【注】此格式只修改匹配文檔的第一行,且只留下修改的欄位,其他欄位均刪除。
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1})
#輸出
{ "name" : "root", "password" : "x", "uid" : 0 }
{ "name" : "bin", "password" : "x", "uid" : 1 }
{ "name" : "daemon", "password" : "x", "uid" : 2 }
{ "name" : "adm", "password" : "x", "uid" : 3 }
db.passwd.update({uid:{$lte:3}},{password:"888"}) #更新
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1})
#再次查詢,輸出
{ "name" : "bin", "password" : "x", "uid" : 1 }
{ "name" : "daemon", "password" : "x", "uid" : 2 }
{ "name" : "adm", "password" : "x", "uid" : 3 }
db.passwd.find({password:"888"})
#修改的輸出
{ "_id" : ObjectId("5b861362798eac5b9bacbb9b"), "password" : "888" }
(2) update({條件},{$set:{修改的欄位} })
【注】此格式只修改匹配文檔的第一行,且只修改匹配的欄位,其他欄位不變。
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1}) #查詢
{ "name" : "bin", "password" : "x", "uid" : 1 }
{ "name" : "daemon", "password" : "x", "uid" : 2 }
{ "name" : "adm", "password" : "x", "uid" : 3 }
db.passwd.update({uid:{$lte:3}},{$set:{password:"999"}}) #更新
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1}) #再次查詢
{ "name" : "bin", "password" : "999", "uid" : 1 } #只更新了這一行
{ "name" : "daemon", "password" : "x", "uid" : 2 }
{ "name" : "adm", "password" : "x", "uid" : 3 }
(3) update({條件},{$set:{修改的欄位} }, false, true)
【注】此格式修改所有的匹配文檔,且只修改匹配的欄位,其他欄位不變。
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1}) #查詢
{ "name" : "bin", "password" : "999", "uid" : 1 }
{ "name" : "daemon", "password" : "x", "uid" : 2 }
{ "name" : "adm", "password" : "x", "uid" : 3 }
db.passwd.update({uid:{$lte:3}},{$set:{password:"FFF",gid:7}},false,true) #更新所有文檔
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1,gid:1}) #更新後再次查詢
{ "name" : "bin", "password" : "FFF", "uid" : 1, "gid" : 7 } #所有行都更新了
{ "name" : "daemon", "password" : "FFF", "uid" : 2, "gid" : 7 }
{ "name" : "adm", "password" : "FFF", "uid" : 3, "gid" : 7 }
(4) 刪除與條件匹配的文檔欄位 update({條件},{$unset:{修改的欄位} })
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1,gid:1}) #查詢
{ "name" : "bin", "password" : "FFF", "uid" : 1, "gid" : 7 }
{ "name" : "daemon", "password" : "FFF", "uid" : 2, "gid" : 7 }
{ "name" : "adm", "password" : "FFF", "uid" : 3, "gid" : 7 }
db.passwd.update({uid:{$lte:3}},{$unset:{gid:7}}) #刪除gid為7的列
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1,gid:1}) #刪除後查詢
{ "name" : "bin", "password" : "FFF", "uid" : 1 } #只刪除了第一個匹配的文檔的列
{ "name" : "daemon", "password" : "FFF", "uid" : 2, "gid" : 7 }
{ "name" : "adm", "password" : "FFF", "uid" : 3, "gid" : 7 }
db.passwd.update({uid:{$lte:3}},{$unset:{gid:7}},false,true) #加上false,true參數
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 2 })
db.passwd.find({uid:{$lte:3}},{_id:0,name:1,uid:1,password:1,gid:1}) #再次查詢
{ "name" : "bin", "password" : "FFF", "uid" : 1 } #刪除了所有匹配文檔的列
{ "name" : "daemon", "password" : "FFF", "uid" : 2 }
{ "name" : "adm", "password" : "FFF", "uid" : 3 }
(5) 欄位值自加或自減 update({條件},{$inc:{欄位名:數字} })
db.passwd.update({ uid:{$lte:5} },{ $inc:{uid:1} }) #uid自加1,只更新第一行
db.passwd.update({ uid:{$lte:5} },{$inc:{uid:1}} , false, true ) #uid自加1,更新所有行
db.passwd.update({ uid:{$lte:5} },{$inc:{uid:-2}}, false, true ) #uid自減2,更新所有行
(6) 更新數組 $push $addToSet $pop $pull
db.aaa.insert({name:"tom",hobby:["sing","tennis","movie"]}) #插入
db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1})
#輸出
{ "name" : "tom", "hobby" : [ "sing", "tennis", "movie" ] }
db.aaa.update({name:"tom"},{$push:{hobby:"dance"}}) #數組插入push
db.aaa.update({name:"tom"},{$push:{hobby:"dance"}}) #push可插入相同值
db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1})
#輸出
{ "name" : "tom", "hobby" : [ "sing", "tennis", "movie", "dance", "dance" ] }
db.aaa.update({name:"tom"},{$addToSet:{hobby:"dance"}}) #數組插入addToSet
db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1})
#查詢可知,重複值插入不成功
{ "name" : "tom", "hobby" : [ "sing", "tennis", "movie", "dance", "dance" ] }
db.aaa.update({name:"tom"},{$addToSet:{hobby:"game"}}) #插入非重複值
db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1})
#輸出,插入成功
{ "name" : "tom", "hobby" : [ "sing", "tennis", "movie", "dance", "dance", "game" ] }
db.aaa.update({name:"tom"},{$pop:{hobby:1}}) #刪除數組尾部元素pop 1
db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1})
#輸出
{ "name" : "tom", "hobby" : [ "sing", "tennis", "movie", "dance", "dance" ] }
db.aaa.update({name:"tom"},{$pop:{hobby:-1}}) #刪除數組頭部元素 pop -1
db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1})
#輸出
{ "name" : "tom", "hobby" : [ "tennis", "movie", "dance", "dance" ] }
db.aaa.update({name:"tom"},{$pull:{hobby:"dance"}}) #刪除數組某個元素pull
db.aaa.find({name:"tom"},{_id:0,name:1,hobby:1})
#輸出
{ "name" : "tom", "hobby" : [ "tennis", "movie" ] }
4. 刪除文檔
- 刪除集合同時刪除索引:$drop()
- 刪除文檔不刪除索引:$remove()
db.bbb.drop() #刪除bbb集合
db.passwd.remove({name:"ftp"}) #條件刪除文檔
db.passwd.remove({}) #刪除所有文檔
※利用OpenSSL實現非阻塞通訊C++代碼
※CSS3 3D transform實現旋轉木馬效果
TAG:程序員小新人學習 |