一、基礎(chǔ)
1、增
# 創(chuàng)建庫(kù) ps:use的話(huà)需要插入數(shù)據(jù)才能顯示的喲!
> use msg
# 查看當(dāng)前使用的庫(kù)
> db
# 創(chuàng)建集合
> use msg
> db.createCollection("user")
> show tables
# 插入數(shù)據(jù)
> db.user.insert({"name":"mongo"})
2、刪
# 刪除庫(kù)
> use msg
> db.dropDatabase()
# 刪除集合
> use msg
> db.user.drop()
# 刪除整個(gè)集合中的所有數(shù)據(jù)
> db.test.remove()
# 刪除集合中符合過(guò)濾條件的數(shù)據(jù)
> db.test.remove({name:/aws/})
# 刪除符合條件的一條記錄
> db.test.remove({name:/aws/},1)
3、查
# 查詢(xún)數(shù)據(jù)庫(kù)
> use msg
# 查看數(shù)據(jù)庫(kù)數(shù)據(jù)詳情
> db.stats()
# 查詢(xún)集合
> show tables
# 重命名集合
> db.user.renameCollection("USER")
# 查看集合數(shù)據(jù)詳情
> db.user.stats()
# 查看集合所有數(shù)據(jù)
> db.user.find()
# 格式化輸出
> db.user.find().pretty()
# 查看一條數(shù)據(jù)
> db.user.findOne()
# 查看匹配數(shù)據(jù)
> db.user.find({"name":"mongo"})
# 查詢(xún)大于某個(gè)數(shù)值數(shù)據(jù)
> db.user.find({salary:{$gt:7000}})
# 查看名稱(chēng)中包含‘a(chǎn)’的數(shù)據(jù)
> db.user.find({name:/a/})
# 查詢(xún)name以W打頭的數(shù)據(jù)
> db.user.find({name:/^W/})
# 多條件“與”
# 查詢(xún)age小于30,salary大于6000的數(shù)據(jù)
> db.user.find({age:{$lt:30},salary:{$gt:6000}})
# 多條件“或”
# 查詢(xún)age小于25,或者salary大于10000的記錄
> db.user.find({$or:[{salary:{$gt:10000}},{age:{$lt:25}}]})
# 查詢(xún)記錄中的指定列
> db.user.find({},{name:1,age:1,salary:1,sex_orientation:true})
# 查詢(xún)指定字段的數(shù)據(jù),并去重
> db.user.distinct('gender')
# 指定結(jié)果集返回條目
> db.user.find().limit(2)
# 查詢(xún)第一條以外的數(shù)據(jù)
> db.user.find().skip(1)
# 對(duì)結(jié)果集排序
# 升序
> db.user.find().sort({salary:1})
# 將序
> db.user.find().sort({salary:-1})
# 統(tǒng)計(jì)記錄數(shù)量
> db.user.find().count()
4、改
# 修改數(shù)據(jù)
> db.user.update({"name":"mongo"},{$set:{"name":"mongos"}})
# 修改多條相同文檔
> db.user.update({"name":"mongo"},{$set:{"name":"mongos"}},{multi:true})
# 只更新第一條記錄:
> db.user.update( { "count" : { $gt : 1 } } , { $set : { "test2" : "OK"} } );
# 全部更新:
> db.user.update( { "count" : { $gt : 3 } } , { $set : { "test2" : "OK"} },false,true );
# 只添加第一條:
> db.user.update( { "count" : { $gt : 4 } } , { $set : { "test5" : "OK"} },true,false );
# 全部添加進(jìn)去:
> db.user.update( { "count" : { $gt : 5 } } , { $set : { "test5" : "OK"} },true,true );
# 全部更新:
> db.user.update( { "count" : { $gt : 15 } } , { $inc : { "count" : 1} },false,true );
# 只更新第一條記錄:
> db.user.update( { "count" : { $gt : 10 } } , { $inc : { "count" : 1} },false,false );
# 更新符合過(guò)濾條件的信息
> db.user.update({name:'Gal Gadot'},{$set:{age:23}},false,true)
# 添加新的字段
> db.user.update({name:'Mikie Hara'},{$set:{interest:"CBA"}},false,true)
# 使用函數(shù) $inc
> db.user.update({gender:'female'},{$inc:{salary:50}},false,true)
# 原
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female","interest" : "CBA", "age" : 26, "salary" : 7000 }
# 現(xiàn)
{ "_id" : ObjectId("52453d8525e437dfea8fd4f5"), "name" : "Mikie Hara", "gender" : "female","interest" : "CBA", "age" : 26, "salary" : 7050 }
二、集合操作
| 方法名 |
描述 |
| db.collection.aggregate() |
聚合,主要用于處理數(shù)據(jù)(諸如統(tǒng)計(jì)平均值,求和等),并返回計(jì)算后的數(shù)據(jù)結(jié)果 |
| db.collection.bulkWrite() |
批量寫(xiě)入 |
| db.collection.createIndex() |
創(chuàng)建一個(gè)集合索引 |
| db.collection.count() |
返回集合總數(shù)或匹配查詢(xún)的結(jié)果集總數(shù) |
| db.collection.deleteOne() |
刪除集合中的一個(gè)文檔 |
| db.collection.deleteMany() |
刪除集合中的多個(gè)文檔 |
| db.collection.dataSize() |
返回集合的大小 |
| db.collection.distinct() |
返回具有指定字段不同值的文檔(去除指定字段的重復(fù)數(shù)據(jù)) |
| db.collection.dropIndex() |
刪除一個(gè)集合中的指定索引 |
| db.collection.dropIndexes() |
刪除一個(gè)集合中的所有索引 |
| db.collection.drop() |
刪除當(dāng)前數(shù)據(jù)庫(kù)中的collection集合 |
| db.collection.explain() |
返回各種方法的查詢(xún)執(zhí)行信息 |
| db.collection.findOne() |
查詢(xún)單條數(shù)據(jù) |
| db.collection.findOneAndReplace() |
查詢(xún)單條數(shù)據(jù)并替換 |
| db.collection.findOneAndDelete() |
查詢(xún)單條數(shù)據(jù)并刪除 |
| db.collection.findOneAndUpdate() |
查詢(xún)單條數(shù)據(jù)并更新 |
| db.collection.find() |
查詢(xún)集合,無(wú)參數(shù)則查詢(xún)所有,并返回一個(gè)游標(biāo)對(duì)象 |
| db.collection.findAndModify() |
查詢(xún)并修改 |
| db.collection.getIndexes() |
返回當(dāng)前集合的所有索引數(shù)組 |
| db.collection.group() |
提供簡(jiǎn)單的數(shù)據(jù)聚合功能 |
| db.collection.isCapped() |
判斷集合是否為定容量 |
| db.collection.insert() |
在當(dāng)前集合插入一條或多條數(shù)據(jù)(或叫文檔) |
| db.collection.insertMany() |
在當(dāng)前集合插入多條數(shù)據(jù) |
| db.collection.insertOne() |
在當(dāng)前集合插入一條數(shù)據(jù) |
| db.collection.reIndex() |
重建當(dāng)前集合的所有索引 |
| db.collection.renameCollection() |
重命名集合名稱(chēng) |
| db.collection.replaceOne() |
替換集合中的一個(gè)文檔(一條數(shù)據(jù)) |
| db.collection.remove() |
從當(dāng)前集合刪除數(shù)據(jù) |
| db.collection.save() |
已插入數(shù)據(jù)更新 |
| db.collection.stats() |
返回當(dāng)前集合的狀態(tài) |
| db.collection.storageSize() |
返回當(dāng)前集合已使用的空間大小 |
| db.collection.totalSize() |
返回當(dāng)前集合的總占用空間,包括所有文件和所有索引 |
| db.collection.totalIndexSize() |
返回當(dāng)前集合所有的索引所占用的空間大小 |
| db.collection.updateMany() |
修改集合中的多條數(shù)據(jù) |
| db.collection.update() |
修改集合中的數(shù)據(jù) |
| db.collection.updateOne() |
修改集合中的一條數(shù)據(jù) |
| db.collection.validate() |
執(zhí)行對(duì)集合驗(yàn)證操作 |
三、副本集操作
| 方法名 |
描述 |
| rs.initiate() |
|
| rs.addArb() |
添加仲裁 |
| rs.help() |
幫助 |
| rs.printReplicationInfo() |
查看到副本集操作日志 |
| rs.remove() |
減少副本集節(jié)點(diǎn) |
| rs.freeze() |
“凍結(jié)”mongodb實(shí)例 |
| rs.status()/sh.status() |
查看副本集狀態(tài) |
| db.printSlaveReplicationInfo |
查看復(fù)制集的同步狀態(tài) |
| rs.add() |
增加副本集節(jié)點(diǎn) |
| rs.stepDown() |
將當(dāng)前主庫(kù)“降級(jí)” |
| rs.slaveOk() |
|
| rs.conf() |
|
| rs.syncFrom() |
|
| rs.reconfig() |
|
四、用戶(hù)管理
| 方法名 |
描述 |
| db.getUsers() |
獲取所有用戶(hù) |
| db.dropAllUsers() |
刪除所有用戶(hù) |
| db.updateUser() |
更新用戶(hù) |
| db.createUser() |
創(chuàng)建用戶(hù) |
| db.revokeRolesFromUser() |
取消授權(quán) |
| db.removeUser() |
刪除用戶(hù) |
| db.grantRolesToUser() |
授權(quán) |
| db.getUser() |
獲取用戶(hù) |
| db.changeUserPassword() |
更改用戶(hù)密碼 |
| db.auth() |
數(shù)據(jù)庫(kù)認(rèn)證、安全模式 |
五、索引
1、新增索引
# 單鍵索引
> db.集合.createIndex({"name":1})
# 倒序索引
> db.集合.createIndex({"name":-1})
# 唯一索引
> db.集合.createIndex({"name":1},{"unique":true})
# 后臺(tái)建索引
> db.集合.createIndex({"name":1},{background:1})
# 復(fù)合索引
> db.集合.createIndex({"name":1,"age":1})
2、查看索引
# 查看數(shù)據(jù)庫(kù)所有索引
> db.system.indexes.find()
# 查看集合索引
> db.集合.getIndexes()
> db.集合.getIndexKeys()
# 查看索引大小
> db.集合.totalIndexSize()
3、刪除索引
# 刪除單個(gè)索引
> db.集合.dropIndex("name_1")
# 刪除所有索引
> db.集合.dropIndexes()
4、重建索引
> db.集合.reIndex({"name":1})
六、副本集
1、基礎(chǔ)操作
# 查看集群狀態(tài)
> rs.status()
# 查看配置
> rs.config()
# 允許在從節(jié)點(diǎn)查詢(xún)操作
> rs.slaveOK()
# 查詢(xún)是否主節(jié)點(diǎn)
> rs.isMaster()
2、成員管理
# 添加成員
> rs.add("ip:port")
# 添加仲裁節(jié)點(diǎn)
> rs.addArb("ip:port")
# 刪除成員
> rs.remove("ip:port")
# 重置配置
> rs.reconfig()
3、主從管理
# 降級(jí)
> rs.stepDown(time)
# 阻止選舉
> rs.freeze(time)
# 主從延遲狀態(tài)
# 主
> db.printReplicationInfo()
# 從
> db.printSlaveReplicationInfo()
七、分片
1、基礎(chǔ)操作
# 查看集群狀態(tài)
mongos> sh.status()
# 列出所有分片
mongos> db.runCommand({ listshards : 1 })
# 添加分片
mongos> sh.addShard("shard1/192.168.200.11:40000")
# 移除分片
mongos> db.runCommand({"removeShard":"shard1"})
# 如果是主分片,需要先把對(duì)應(yīng)數(shù)據(jù)庫(kù)移動(dòng)到其他分片,再刪除,執(zhí)行多遍查看是否遷移完成。
mongos> db.runCommand( { movePrimary: "msg", to: "shard2" })
# 移除完需要刷新緩存
mongos> db.adminCommand({"flushRouterConfig":1})
2、平衡器
# 平衡器
# 開(kāi)啟平衡器
mongos> sh.startBalancer()
# 停止平衡器
mongos> sh.stopBalancer() # 停止平衡器
or
mongos> sh.setBalancerState(false)
# 獲取平衡器狀態(tài)
mongos> sh.getBalancerState()
# 驗(yàn)證禁用后有沒(méi)有正在遷移的塊
mongos> sh.isBalancerRunning()
3、開(kāi)啟分片
mongos> use admin
# 開(kāi)啟庫(kù)分片
mongos> sh.enableSharding("dba")
或
mongos> db.runCommand({enablesharding:"dba"})
# 開(kāi)啟集合分片
mongos> sh.shardCollection("庫(kù).集合",{"name":1})
或
mongos> db.runCommand({shardcollection:"庫(kù).集合",key:{"name":1}})
mongos> sh.status()
# 查看集合分布情況
mongos> db.集合.stats()
4、數(shù)據(jù)塊
# 查看塊大小
mongos> use config
mongos> db.settings.find()
# 修改塊大小
mongos> db.settings.save({"_id":"chunksize","value":32})
手動(dòng)遷移塊
# 需要先關(guān)閉平衡器喲
mongos> use config
查詢(xún)塊
mongos> db.chunks.find().pretty()
#手動(dòng)拆分
mongos> sh.splitAt("庫(kù).集合",{"groupId":"500000001","_id":"6569514481477484544"})
#手動(dòng)遷移分片
mongos> sh.moveChunk("庫(kù).集合",{"name" : "wPeFnJEvendSTbH"},"其他分片")