您好,登錄后才能下訂單哦!
mongodb使用
1)連接mongodb
mongo --port 10001 //指定port,默認port為27017,這樣就連接了本地的mongodb了
mongo --host 192.168.0.11 //連接遠程的mongodb
mongo -umyuser -p123456 //使用用戶名密碼登錄 類似mysql
[root@localhost ~]# mongo --port 27017
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:27017/test
>
[root@localhost ~]# mongo //等同 mongo --port 27017 類似直接mysql直接連接
MongoDB shell version: 3.0.7
connecting to: test
>
[root@localhost ~]# mongo --host 127.0.0.1 #指定主機名字和ip
MongoDB shell version: 3.0.7
connecting to: 127.0.0.1:27017/test
>
[root@localhost ~]# netstat -lnp|grep mongo
tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 3769/mongod
unix 2 [ ACC ] STREAM LISTENING 21107 3769/mongod /tmp/mongodb-27017.sock
[root@localhost ~]# ps aux|grep mongo
mongod 3769 0.5 2.4 500844 47660 ? Sl 10:57 0:17 /usr/bin/mongod -f /etc/mongod.conf
root 7198 0.0 0.0 103308 860 pts/1 S+ 11:54 0:00 grep mongo
2) 用戶管理
用戶角色: http://bbs.51cto.com/thread-1146654-1.html
#創建用戶
首先mongodb用戶是針對庫來的,建立用戶時要先進入對應的庫里
> use test #選擇庫。若是之前庫不存在,會自動創建該庫
switched to db test
> db.createUser({user:"admin",pwd:"123456",roles:[{role:'dbOwner',db:'userdb'}]})
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "dbOwner",
"db" : "userdb"
}
]
}
#查看用戶
db.system.users.find() //列出所有用戶,需要切換到admin庫下(use admin)
> use admin #所有用戶都在這個庫下
switched to db admin
> db.system.users.find()
{ "_id" : "test.admin", "user" : "admin", "db" : "test", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "GXrf4fS8qPBncxKo2ElryA==", "storedKey" : "C7DFv8iY0gxXgFCgQC/jbCGWCMU=", "serverKey" : "FfbqE2RglKh55DOxy4VmN8ptSEA=" } }, "roles" : [ { "role" : "dbOwner", "db" : "userdb" } ] }
>
show users //查看當前庫下所有的用戶
#刪除用戶,需要切換到admin庫
db.dropUser('admin')
> use test #切換到之前建立的庫里面刪除之前新建的用戶
switched to db test
> db.dropUser('admin')
true
> show users
> db.system.users.find() #然后發現什么用戶也沒有
3)數據庫管理
#查看版本
MongoDB shell version: 3.0.7
connecting to: test
> db.version()
3.0.7
# 切換/創建庫
> use userdb //如果庫存在就切換,不存在就創建
switched to db userdb
#此時show dbs 看不到userdb,需要我們創建一個集合
db.createCollection('clo1')
再show dbs就有userdb了。
> use userdb #此時雖然創建了庫但是show的時候是沒有的必須創建一個集合
switched to db userdb
> show dbs
admin 0.078GB
local 0.078GB
> db.createCollection('clo1') #創建集合,集合的名字是clo1
{ "ok" : 1 }
> show dbs
admin 0.078GB
local 0.078GB
userdb 0.078GB #此時會顯示庫了
>
#查看所有數據庫
show dbs
#刪除數據庫 mydb
> show dbs
admin 0.078GB
local 0.078GB
userdb 0.078GB
> use userdb #先切換到該庫下
switched to db userdb
> db.dropDatabase()
{ "dropped" : "userdb", "ok" : 1 }
> show dbs
admin 0.078GB
local 0.078GB #此時就沒有了之前刪除了userdb庫了
#查看當前庫信息
> use admin // 先切換到該庫
switched to db admin
> db.stats()
{
"db" : "admin",
"collections" : 4,
"objects" : 10,
"avgObjSize" : 67.2,
"dataSize" : 672,
"storageSize" : 28672,
"numExtents" : 4,
"indexes" : 3,
"indexSize" : 24528,
"fileSize" : 67108864,
"nsSizeMB" : 16,
"extentFreeList" : {
"num" : 0,
"totalSize" : 0
},
"dataFileVersion" : {
"major" : 4,
"minor" : 22
},
"ok" : 1
}
>
#查看mongodb服務器狀態
> db.serverStatus()
{
"host" : "localhost.localdomain",
"version" : "3.0.7",
"process" : "mongod",
"pid" : NumberLong(3769),
"uptime" : 15510,
"uptimeMillis" : NumberLong(15509761),
"uptimeEstimate" : 14138,
"localTime" : ISODate("2015-12-14T07:16:10.249Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 0,
"rollovers" : 0
},
"backgroundFlushing" : {
"flushes" : 258,
"total_ms" : 25,
"average_ms" : 0.09689922480620156,
"last_ms" : 0,
"last_finished" : ISODate("2015-12-14T07:15:40.721Z")
},
。。。。。。。
4)數據管理
創建集合
> use mydb
switched to db mydb
> db.createCollection("mycol", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } )
{ "ok" : 1 }
#mycol 是集合名字
語法:db.createCollection(name,options)
name就是集合的名字,options可選,用來配置集合的參數,參數如下
capped true/false (可選)如果為true,則啟用封頂集合。封頂集合是固定大小的集合,會自動覆蓋最早的條目,當它達到其最大大小。如果指定true,則需要也指定尺寸參數。
autoindexID true/false (可選)如果為true,自動創建索引_id字段的默認值是false。
size (可選)指定最大大小字節封頂集合。如果封頂如果是 true,那么你還需要指定這個字段。單位B
max (可選)指定封頂集合允許在文件的最大數量。
> db.createCollection('caimz')
{ "ok" : 1 }
> show collections
caimz
mycol
system.indexes
查看集合
# show collections
> show collections
caimz
mycol
system.indexes
> show tables
caimz
mycol
system.indexes
在集合中添加文檔
db.Account.insert({AccountID:2,UserName:"123",password:"123456"}) //如果集合不存在,直接插入數據,則mongodb會自動創建集合
> db.Account.insert({AccountID:2,UserName:"123",password:"123456"})
WriteResult({ "nInserted" : 1 })
> show tables
Account
caimz
mycol
system.indexes
>
修改
db.Account.update({AccountID:2},{"$set":{"Age":20}})
查看
db.Account.find() //查看所有文檔
> db.Account.insert({AccountID:2,UserName:"aaa",password:"123456"})
WriteResult({ "nInserted" : 1 })
> db.Account.find()
{ "_id" : ObjectId("566e6f1f269ae27bd4c1929e"), "AccountID" : 2, "UserName" : "123", "password" : "123456" }
{ "_id" : ObjectId("566e6f7f269ae27bd4c1929f"), "AccountID" : 2, "UserName" : "aaa", "password" : "123456" }
db.Account.find({AccountID:2}) //根據條件查詢
> db.Account.find()
{ "_id" : ObjectId("566e6f1f269ae27bd4c1929e"), "AccountID" : 2, "UserName" : "123", "password" : "123456" }
{ "_id" : ObjectId("566e6f7f269ae27bd4c1929f"), "AccountID" : 2, "UserName" : "aaa", "password" : "123456" }
{ "_id" : ObjectId("566e6fab269ae27bd4c192a0"), "AccountID" : 1, "UserName" : "aaa", "password" : "123456" }
> db.Account.find({AccountID:1})
{ "_id" : ObjectId("566e6fab269ae27bd4c192a0"), "AccountID" : 1, "UserName" : "aaa", "password" : "123456" }
> db.caimz.find({UserName:"2"})
{ "_id" : ObjectId("566e7049c796070f15a8a72e"), "AccountID" : 2, "UserName" : "2", "password" : "123456" }
更新:
> db.Account.update({AccountID:1},{"$set":{"Age":20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.Account.find()
{ "_id" : ObjectId("566e6f1f269ae27bd4c1929e"), "AccountID" : 2, "UserName" : "123", "password" : "123456" }
{ "_id" : ObjectId("566e6f7f269ae27bd4c1929f"), "AccountID" : 2, "UserName" : "aaa", "password" : "123456" }
{ "_id" : ObjectId("566e6fab269ae27bd4c192a0"), "AccountID" : 1, "UserName" : "aaa", "password" : "123456", "Age" : 20 }
> db.caimz.update({AccountID:1},{"$set":{"Age":19}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.caimz.find()
{ "_id" : ObjectId("566e7042c796070f15a8a72d"), "AccountID" : 1, "UserName" : "1", "password" : "123456", "Age" : 19 }
{ "_id" : ObjectId("566e7049c796070f15a8a72e"), "AccountID" : 2, "UserName" : "2", "password" : "123456" }
> db.caimz.update({AccountID:1},{"$set":{age:19}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.caimz.find()
{ "_id" : ObjectId("566e7042c796070f15a8a72d"), "AccountID" : 1, "UserName" : "1", "password" : "123456", "Age" : 19, "age" : 19 }
{ "_id" : ObjectId("566e7049c796070f15a8a72e"), "AccountID" : 2, "UserName" : "2", "password" : "123456" }
>
刪除
db.Account.remove({AccountID:2}) //根據條件刪除
> db.caimz.find()
{ "_id" : ObjectId("566e7042c796070f15a8a72d"), "AccountID" : 1, "UserName" : "1", "password" : "123456", "Age" : 1, "age" : 19 }
{ "_id" : ObjectId("566e7049c796070f15a8a72e"), "AccountID" : 2, "UserName" : "2", "password" : "123456" }
> db.caimz.remove({age:19})
WriteResult({ "nRemoved" : 1 })
> db.caimz.find()
{ "_id" : ObjectId("566e7049c796070f15a8a72e"), "AccountID" : 2, "UserName" : "2", "password" : "123456" }
>
要刪除整個文檔
db.Account.drop()
> show tables
Account
caimz
mycol
system.indexes
> db.Account.drop()
true
> show tables
caimz
mycol
system.indexes
>
查看集合的狀態
use dbname //先進入對應的庫,然后查看集合狀態
db.printCollectionStats()
5)數據庫性能
db.stats() //查看當前庫的信息
db.serverStatus() //查看mongodb服務器的狀態 列出庫里面的所以collection
> db.printCollectionStats()
caimz
{
"ns" : "mydb.caimz",
"count" : 1,
"size" : 112,
"avgObjSize" : 112,
"numExtents" : 1,
"storageSize" : 8192,
"lastExtentSize" : 8192,
"paddingFactor" : 1,
"paddingFactorNote" : "paddingFactor is unused and unmaintained in 3.0. It remains hard coded to 1.0 for compatibility only.",
"userFlags" : 1,
。。。。。。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。