mongodb基礎操做

1.與數據庫相關

(1)切換/建立數據庫數據庫

> use ikidana; switched to db ikidana

當數據庫不存在的時候,就會執行建立操做。工具

若是你建立了一個數據庫可是並無向裏面寫入任何數據或者建立任何用戶,那麼這個數據庫並無真正被建立出來。post

 

(2)查詢全部數據庫spa

> show dbs; admin 0.000GB ikidana 0.000GB local 0.000GB

 

(3)刪除當前使用的數據庫rest

> show dbs admin 0.000GB ikidana 0.000GB local 0.000GB test 0.000GB > db.dropDatabase(); { "dropped" : "test", "ok" : 1 } > show dbs admin 0.000GB ikidana 0.000GB local 0.000GB

 

(4)查詢當前使用的數據庫code

> db admin > db.getName() admin

 

(5)顯示當前DB的狀態server

> db.stats() { "db" : "admin", "collections" : 2, "views" : 0, "objects" : 4, "avgObjSize" : 182.5, "dataSize" : 730, "storageSize" : 65536, "numExtents" : 0, "indexes" : 4, "indexSize" : 131072, "ok" : 1 }

 

(6)當前DB的版本blog

> db.version() 3.4.22

 

2.與用戶相關

MongoDB role類型:索引

  • 數據庫用戶角色(Database User Roles)

    read:授予User只讀數據的權限
    readWrite:授予User讀寫數據的權限rem

  • 數據庫管理角色(Database Administration Roles):

    dbAdmin:在當前dB中執行管理操做
    dbOwner:在當前DB中執行任意操做
    userAdmin:在當前DB中管理User

  • 備份和還原角色(Backup and Restoration Roles):

    backup
    restore

  • 跨庫角色(All-Database Roles):

    readAnyDatabase:授予在全部數據庫上讀取數據的權限
    readWriteAnyDatabase:授予在全部數據庫上讀寫數據的權限
    userAdminAnyDatabase:授予在全部數據庫上管理User的權限
    dbAdminAnyDatabase:授予管理全部數據庫的權限

  • 集羣管理角色(Cluster Administration Roles):

    clusterAdmin:授予管理集羣的最高權限
    clusterManager:授予管理和監控集羣的權限
    clusterMonitor:授予監控集羣的權限,對監控工具具備readonly的權限
    hostManager:管理Server

(1)查詢當前數據庫的全部用戶

> use ikidana; switched to db ikidana > show users; { "_id" : "ikidana.kebi", "userId" : BinData(4,"8yldjVL8TxKjwNYhpmZ+Yg=="), "user" : "kebi", "db" : "ikidana", "roles" : [ { "role" : "dbAdmin", "db" : "ikidana" } ] } { "_id" : "ikidana.yueji", "userId" : BinData(4,"OIbrRRo+QwmhZmw2ECekPQ=="), "user" : "yueji", "db" : "ikidana", "roles" : [ { "role" : "readWrite", "db" : "ikidana" } ] }

 

(2)建立管理員用戶

> use admin switched to db admin > db.createUser({user:"admin",pwd:"password",roles:["root"]}) Successfully added user: { "user" : "admin", "roles" : [ "root" ] }

 

(3)認證登陸(當前數據庫的用戶)

> db.auth("kebi","123") 1
> db.auth("kebi","123456") Error: Authentication failed. 0

 

(4)添加數據庫用戶

> db.createUser({user:"yueji",pwd:"123456",roles:[{role:"readWrite",db:"ikidana"}]}) Successfully added user: { "user" : "yueji", "roles" : [ { "role" : "readWrite", "db" : "ikidana" } ] }

 

(5)查看系統用戶

> db.system.users.find() { "_id" : "admin.admin", "userId" : BinData(4,"6QN49Zz2T/qrReics+1kIQ=="), "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "lCQUD/gOOtWhah/I86pnbQ==", "storedKey" : "EbuFk6exHsvDzpe65P6oCqUOGDU=", "serverKey" : "sLj3qBhj2FBXJ3xhtapq4fRzT6g=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] } { "_id" : "admin.yueji", "userId" : BinData(4,"a7Jqzjg8RD6LewZU5sh4RA=="), "user" : "yueji", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "8KKhrQ2XFgk/9iJQbGTDxA==", "storedKey" : "4CzO4FdMnbq20IoW1+zXBJMiBQA=", "serverKey" : "TwQSkGtHpsY8a9WTaWyLfIPIzS0=" } }, "roles" : [ { "role" : "read", "db" : "admin" } ] } { "_id" : "ikidana.yueji", "userId" : BinData(4,"OIbrRRo+QwmhZmw2ECekPQ=="), "user" : "yueji", "db" : "ikidana", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "BXriW8BvaltTyHbrg7q1Hw==", "storedKey" : "W5EnO6ICteuuCDELhFO5OljKUuw=", "serverKey" : "DVZqiufUt8DD4T6UHhNLoTRHAR0=" } }, "roles" : [ { "role" : "readWrite", "db" : "ikidana" } ] } { "_id" : "ikidana.kebi", "userId" : BinData(4,"8yldjVL8TxKjwNYhpmZ+Yg=="), "user" : "kebi", "db" : "ikidana", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "bToeXegvlq9Zf6OgyyAE8A==", "storedKey" : "YpgLtzd/z3JBz/memJatHRr5/WI=", "serverKey" : "3VG8GrVgkUEtwc5zc4iJScaEEmM=" } }, "roles" : [ { "role" : "dbAdmin", "db" : "ikidana" } ] }

 

(6)刪除用戶

> use ikidana; switched to db ikidana > show users; { "_id" : "ikidana.kebi", "userId" : BinData(4,"8yldjVL8TxKjwNYhpmZ+Yg=="), "user" : "kebi", "db" : "ikidana", "roles" : [ { "role" : "dbAdmin", "db" : "ikidana" } ] } { "_id" : "ikidana.yueji", "userId" : BinData(4,"OIbrRRo+QwmhZmw2ECekPQ=="), "user" : "yueji", "db" : "ikidana", "roles" : [ { "role" : "readWrite", "db" : "ikidana" } ] } > db.dropUser("kebi") true
> show users; { "_id" : "ikidana.yueji", "userId" : BinData(4,"OIbrRRo+QwmhZmw2ECekPQ=="), "user" : "yueji", "db" : "ikidana", "roles" : [ { "role" : "readWrite", "db" : "ikidana" } ] }

 

3.與Collection彙集集合

(1)建立一個彙集集合

> db.createCollection("yue-test") { "ok" : 1 }

 

(2)獲得指定名稱的彙集集合

> db.getCollection("info") ikidana.info

 

(3)獲得當前db的全部彙集集合

> db.getCollectionNames()
[ "info", "test", "yue-test" ]
> show collections
info
test
yue-test

 

(4)顯示當前db全部彙集索引的狀態

db.printCollectionStats();

 

4.文檔操做

(1)插入文檔

單條插入:

> db.createCollection("blog") { "ok" : 1 } > post={ ... "title":"blog post", ... "content":"blog content", ... "date":"2011-12-12" ... } { "title" : "blog post", "content" : "blog content", "date" : "2011-12-12" } > db.blog.insert(post) WriteResult({ "nInserted" : 1 })

多條一塊兒插入:

> post1={ ... "title":"blog post1", ... "content":"blog content", ... "date":"2011-12-12" ... } { "title" : "blog post1", "content" : "blog content", "date" : "2011-12-12" } > post2={ ... "title":"blog post2", ... "content":"blog content", ... "date":"2011-12-12" ... } { "title" : "blog post2", "content" : "blog content", "date" : "2011-12-12" } > db.blog.insert([post1,post2]) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 2, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })

 

(2)查看文檔

查看某一個集合的全部文檔:

> db.blog.find() { "_id" : ObjectId("5d424e69828c9a3faa37cd5e"), "title" : "blog post", "content" : "blog content", "date" : "2011-12-12" } { "_id" : ObjectId("5d424e8d828c9a3faa37cd5f"), "title" : "blog post1", "content" : "blog content", "date" : "2011-12-12" } { "_id" : ObjectId("5d424e8d828c9a3faa37cd60"), "title" : "blog post2", "content" : "blog content", "date" : "2011-12-12" }

查看某一個集合的全部文檔,並格式化顯示:

> db.blog.find().pretty() { "_id" : ObjectId("5d424e69828c9a3faa37cd5e"), "title" : "blog post", "content" : "blog content", "date" : "2011-12-12" } { "_id" : ObjectId("5d424e8d828c9a3faa37cd5f"), "title" : "blog post1", "content" : "blog content", "date" : "2011-12-12" } { "_id" : ObjectId("5d424e8d828c9a3faa37cd60"), "title" : "blog post2", "content" : "blog content", "date" : "2011-12-12" }

 

(3)刪除文檔

> db.blog.find() { "_id" : ObjectId("5d424e69828c9a3faa37cd5e"), "title" : "blog post", "content" : "blog content", "date" : "2011-12-12" } { "_id" : ObjectId("5d424e8d828c9a3faa37cd5f"), "title" : "blog post1", "content" : "blog content", "date" : "2011-12-12" } { "_id" : ObjectId("5d424e8d828c9a3faa37cd60"), "title" : "blog post2", "content" : "blog content", "date" : "2011-12-12" } > db.blog.remove({"title":"blog post"}) WriteResult({ "nRemoved" : 1 }) > db.blog.find() { "_id" : ObjectId("5d424e8d828c9a3faa37cd5f"), "title" : "blog post1", "content" : "blog content", "date" : "2011-12-12" } { "_id" : ObjectId("5d424e8d828c9a3faa37cd60"), "title" : "blog post2", "content" : "blog content", "date" : "2011-12-12" }

刪除文檔的方式是很是靈活的,你能夠按照文檔中的任意屬性來進行刪除操做。

相關文章
相關標籤/搜索