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
>
创新互联公司专业为企业提供南票网站建设、南票做网站、南票网站设计、南票网站制作等企业网站建设、网页设计与制作、南票企业网站模板建站服务,10年南票做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
[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,
。。。。。。