MongoDB資料庫 : 管道,使用者管理,複本集等

來源:互聯網
上載者:User

標籤:角色   刪除   bind   slave   _id   拆分   多少   服務   mongod   

彙總(aggregate): db.集合.aggregate([{管道:{運算式}}])

db.集合.aggregate([
{管道1:{運算式1}},
{管道2:{運算式2}},
... ...
])

管道1的結果作為管道2的輸入.

$group:分組 {$group:{
_id:‘$gender‘,
count:{$sum:1}
}}

$match:過濾(年齡大於19) {$match:{age:{$gt:19}}}

$project:投影(和find第二組參數一樣,1顯示,0不顯示) {$project:{_id:0,counter:1}}

$sort:排序(和sort一樣,1升序,-1降序) {$sort:{_id:-1}}

$skip(跳過多少條文檔,和skip一樣) {$skip:2}

$limit(擷取多少條文檔,和limit一樣) {$limit:2}

$unwind(可以把數組拆分成單條文檔) {$unwind:‘$xxx‘}

索引(1:升序索引,2:降序索引):db.集合.ensureIndex({屬性1:1,屬性2:1})

查看文檔索引 db.集合.getIndex()

刪除索引 db.集合.dropIndex(‘索引名稱‘)

角色: root 只在admin資料庫可用,超級帳號
Read:允許使用者讀取指定資料庫
readWrite:允許使用者讀寫指定資料庫

建立超級管理員:
db.createUser({user:‘admin‘,pwd:‘123‘,roles:[{role:‘root‘,db:‘admin‘}]})

啟用驗證 修改/etc/mongodb.conf:

security:
authorization: enabled

或者 auth=true

用超級管理員建立普通使用者:
db.createUser({user:‘user1‘,pwd:‘123‘,roles:[{role:‘readWrite‘,db:‘students‘}]})

登陸: mongo -u user1 -p 123 --authenticationDatabase students

複本集:資料備份等:
mongod --bind_ip x.x.x.x --porx 27018 --dbpath ~/Desktop/t1 --replSet rs0
mongod --bind_ip x.x.x.x --porx 27019 --dbpath ~/Desktop/t2 --replSet rs0

串連 mongo --host x.x.x.x --port 27018

初始化主伺服器: >rs.initiate() 查看狀態 rs.status()

添加複本集: rs.add(‘x.x.x.x:27019‘)‘)
刪除複本集:rs.remove(‘x.x.x.x:27019‘)‘)

在從伺服器中讀取操作需要設定 rs.slaveOk()

自動主從切換:一個伺服器關閉另一個自動切換成主伺服器.

Database Backup:mongodump -u user1 -p 123 --authenticationDatabase 資料庫名 -d 備份資料庫 -o 存放位置

資料庫恢複: mongorestore -h 伺服器位址 -d 需要恢複的資料庫 --dir 備份資料庫位置

MongoDB資料庫 : 管道,使用者管理,複本集等

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.