mongodb 安装
#下载:
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz
#解压
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz
#移动文件
mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb
#启动mongodb
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/dblogs --fork
#编辑配置文件,使服务器重启后mongodb自动运行
vi /etc/rc.local
#将以下代码放置文件最后并保存退出
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/dblogs --fork
#查看mongodb进程
[root@localhost mongodb]# pstree -p |grep mongod
|-mongod(36708)-+-{mongod}(36709)
| |-{mongod}(36710)
| |-{mongod}(36711)
| |-{mongod}(36713)
| |-{mongod}(36714)
| |-{mongod}(36715)
| |-{mongod}(36716)
| |-{mongod}(36717)
| |-{mongod}(36718)
| `-{mongod}(36719)
#关闭mongodb
pkill mongodb
#配置环境变量/etc/profle文件。往里面新增一行:
export PATH=$PATH:/usr/local/mongodb/bin
其中$PATH后面跟的是实际安装的mongodb的bin所在目录
#接着重新加载配置:
source /etc/profile
#进入mongodb
mongo
#退出mongodb
exit
show dbs #查看数据库
db #查看当前数据库
show tables; #查看数据表(集合)
db.c1.insert({name:"user1"}); #往c1表里面插入一条json格式的数据
db.c1.insert({name:"user1",age:"30"}); #插入数据
db.c1.find(); #查看c1表里的数据
db.c1.remove() #删除c1集合里的所有数据
db.c1.update({name:"user1"},{name:"user22"}) #修改数据,其他数据会丢失
db.c1.update({name:"user1"},{$set:{name:"user22"}}) #修改数据
db.c1.find({条件})
db.c1.find({"name":"user1"});
db.c1.findOne({条件})
db.c1.findOne({"name":"user1"});
mongodb 数据类型
1、null
2、布尔类型
3、32位整数
4、64位整数
5、64位浮点数
6、字符串
7、符号
8、objectId类型
9、日期
10、正则表达式
11、代码
12、数组
13、内嵌文档
mogodb 基础操作 CURD
#删除当前数据库
db.dropDatabase();
#创建集合
db.createCollection("c1");
#删除集合
db.c1.drop();
#查看集合状态
> db.c2.stats()
#切换数据库
use dbname
1、insert (插入语句)
db.c1.insert({name:"user1"});
db.c1.insert({_id:1,name:"user1"}); #id相同插入失败
db.c1.save({name:"user1"});
db.c1.save({_id:1,name:"user1"}); #id相同会更新
2、remove #删除
db.c1.remove(); #删除所有数据
db.c1.remove({}); #删除所有数据
db.c1.remove({name:"user1"}); #删除指定数据
3、find() #查询
db.c1.find(); #查询所有数据
db.c1.find({name:"user1"}); #查询指定数据
db.c1.find({name:"user1",{name:1}); #查询指定数据显示指定字段,默认显示id,不要显示id{_id:0}
> db.c1.find({name:"user5"});
{ "_id" : ObjectId("5cfbd362517f0b0332b6a890"), "name" : "user5", "age" : "age5", "sex" : "sex5" }
> db.c1.find({name:"user5"},{name:1});
{ "_id" : ObjectId("5cfbd362517f0b0332b6a890"), "name" : "user5" }
> db.c1.find({name:"user5"},{name:1,_id:0});
{ "name" : "user5" }
> db.c1.find({name:"user5"},{name:1,age:1,_id:0});
{ "name" : "user5", "age" : "age5" }
条件表达式
#大于 $gt
> db.c1.find({num:{$gt:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#大于等于 $gte
> db.c1.find({num:{$gte:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#小于 $lt
> db.c1.find({num:{$lt:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
#小于等于 $lte
> db.c1.find({num:{$lte:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
#等于 :
> db.c1.find({num:5})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
#不等于 $ne
> db.c1.find({num:{$ne:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#查看有多少数据(统计数据)
> db.c1.count()
10
> db.c1.find().count()
10
#排序(升序) sort()
> db.c1.find().sort({num:1});
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#排序(降序) sort()
> db.c1.find().sort({num:-1});
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
#分页显示数据 skip(5)-表示从第5条开始显示,显示3条数据 limit(3)
> db.c1.find().skip(5).limit(3)
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
#分页并排序 sort
> db.c1.find().sort({num:-1}).skip(3).limit(3)
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
#判断数组中是否存在值 $all
> db.c2.find({post:{$all:[3,2]}})
{ "_id" : ObjectId("5cfbe5c9517f0b0332b6a8aa"), "name" : "user1", "post" : [ 1, 2, 3, 4, 5 ] }
#判断字段是否存在 $exists
> db.c2.find({name:{$exists:1}})
{ "_id" : ObjectId("5cfbe5c9517f0b0332b6a8aa"), "name" : "user1", "post" : [ 1, 2, 3, 4, 5 ] }
> db.c2.find({age:{$exists:1}})
>
#取余 取出余2,和余1的数据 $mod
> db.c1.find({num:{$mod:[2,1]}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
#$in 包含
> db.c1.find({num:{$in:[1,2,3]}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
#$nin 不包含
> db.c1.find({num:{$nin:[1,2,3,4,5,6]}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#查询并且条件and(查询两个条件同时满足或者多个条件满足)
> db.c1.find({name:"user2",num:22})
{ "_id" : ObjectId("5cfd2bbbdc4a8ca381ed2710"), "name" : "user2", "age" : "age22", "num" : 22 }
#查询或者$or(查询条件范围内)
> db.c1.find({$or:[{name:"user2"},{name:"user3"},{name:"user5"}]})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfd2bbbdc4a8ca381ed2710"), "name" : "user2", "age" : "age22", "num" : 22 }
#查询$nor(查询条件范围外)
> db.c1.find({$nor:[{name:"user2"},{name:"user3"},{name:"user5"}]})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#查询数组值的个数 $size
> db.c2.find({post:{$size:4}})
{ "_id" : ObjectId("5cfd2e9fdc4a8ca381ed2711"), "name" : "user2", "post" : [ 6, 7, 8, 9 ] }
{ "_id" : ObjectId("5cfd2eabdc4a8ca381ed2712"), "name" : "user2", "post" : [ 16, 17, 18, 19 ] }
> db.c2.find({post:{$size:2}})
{ "_id" : ObjectId("5cfd2f28dc4a8ca381ed2714"), "name" : "user2", "post" : [ 1, 12 ] }
#正则匹配查询
> db.c1.find({name:/user2/})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfd2bbbdc4a8ca381ed2710"), "name" : "user2", "age" : "age22", "num" : 22 }
> db.c1.find({name:/user2/i})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfd2bbbdc4a8ca381ed2710"), "name" : "user2", "age" : "age22", "num" : 22 }
{ "_id" : ObjectId("5cfd30d2dc4a8ca381ed2715"), "name" : "User2", "age" : "age11", "num" : 12 }
#查询取唯一值去掉重复值 distinct
> db.c1.distinct("name")
[
"user1",
"user2",
"user3",
"user4",
"user5",
"user6",
"user7",
"user8",
"user9",
"user10",
"User2"
]
> db.c1.distinct("num")
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 12 ]
#查询多个数组里面的值
#方法一:
> db.c1.find({"post.titl":1})
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "titl" : 1 }, { "age" : 2 }, { "num" : 3 } ] }
方法二:
> db.c1.find({post:{$elemMatch:{titl:1}}})
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "titl" : 1 }, { "age" : 2 }, { "num" : 3 } ] }
#遍历游标,判断是否有数据,一条一条取出
> var x=db.c1.find()
> x.hasNext()
true
> x.next()
{
"_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"),
"name" : "user1",
"post" : [
{
"titl" : 1
},
{
"age" : 2
},
{
"num" : 3
}
]
}
>
#查询null的值
方法一:
> db.c2.find({age:{$type:10}});
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
方法二:
> db.c2.find({age:{$exists:1,$in:[null]}})
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
# $slice 针对数组使用有效
> db.c1.find({name:"user1"},{post:{$slice:1}})#取前1
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "titl" : 1 } ] }
{ "_id" : ObjectId("5cfdea782e9a2c8b9bb2f9de"), "name" : "user1", "post" : [ { "titl" : 12 } ] }
{ "_id" : ObjectId("5cfdeb432e9a2c8b9bb2f9df"), "name" : "user1", "post" : [ { "titl" : 1 } ] }
{ "_id" : ObjectId("5cfdebd12e9a2c8b9bb2f9e0"), "name" : "user1", "post" : [ { "titl" : 11 } ] }
> db.c1.find({name:"user1"},{post:{$slice:2}})#取前2
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "titl" : 1 }, { "age" : 2 } ] }
{ "_id" : ObjectId("5cfdea782e9a2c8b9bb2f9de"), "name" : "user1", "post" : [ { "titl" : 12 }, { "age" : 22 } ] }
{ "_id" : ObjectId("5cfdeb432e9a2c8b9bb2f9df"), "name" : "user1", "post" : [ { "titl" : 1 }, { "age" : 22 } ] }
{ "_id" : ObjectId("5cfdebd12e9a2c8b9bb2f9e0"), "name" : "user1", "post" : [ { "titl" : 11 }, { "age" : 21 } ] }
> db.c1.find({name:"user1"},{post:{$slice:-2}})#取后2
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "age" : 2 }, { "num" : 3 } ] }
{ "_id" : ObjectId("5cfdea782e9a2c8b9bb2f9de"), "name" : "user1", "post" : [ { "age" : 22 }, { "num" : 23 } ] }
{ "_id" : ObjectId("5cfdeb432e9a2c8b9bb2f9df"), "name" : "user1", "post" : [ { "age" : 22 }, { "num" : 3 } ] }
{ "_id" : ObjectId("5cfdebd12e9a2c8b9bb2f9e0"), "name" : "user1", "post" : [ { "age" : 21 }, { "num" : 3 } ] }
> db.c1.find({name:"user1"},{post:{$slice:[1,2]}})#跳过第1个,从第二个开始取
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "age" : 2 }, { "num" : 3 } ] }
{ "_id" : ObjectId("5cfdea782e9a2c8b9bb2f9de"), "name" : "user1", "post" : [ { "age" : 22 }, { "num" : 23 } ] }
{ "_id" : ObjectId("5cfdeb432e9a2c8b9bb2f9df"), "name" : "user1", "post" : [ { "age" : 22 }, { "num" : 3 } ] }
{ "_id" : ObjectId("5cfdebd12e9a2c8b9bb2f9e0"), "name" : "user1", "post" : [ { "age" : 21 }, { "num" : 3 } ] }
4、修改(update)
#把name:user3的这条数据的age值修改成21
> db.c2.update({name:"user3},{"$.age":"21"})
db.collection.update(criteria,objNew,upsert,multi)
参数说明:
criteria:用于设置查询条件的对象
objnew:用于设置更新内容的对象
upsert:如果记录已经存在,更新它否则新增一个记录(0或1)
multi:如果有多个符合条件的记录,全部更新(0或1)
注意:默认情况下,只会更新第一个符合条件的记录
实例操作:
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
#对象不存在新增一个,存在则修改
> db.c2.update({name:"user66"},{name:"user6"},1)
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
{ "_id" : ObjectId("5cff2ddc5ba0016277aac7f4"), "name" : "user66" }
#对象不存在修改失败,存在则修改
> db.c2.update({name:"user6"},{name:"user66"},0)
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
{ "_id" : ObjectId("5cff2ddc5ba0016277aac7f4"), "name" : "user66" }
#更新所有配置出的对象,
# $set 魔术变量,第四个参数必需存在才能使用
> db.c2.update({name:"user6"},{$set:{name:"user66"}},0,1)
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
{ "_id" : ObjectId("5cff2ddc5ba0016277aac7f4"), "name" : "user66" }
# $set 更新的对象存在则更新,不存在则新一个对象
> db.c2.update({name:"user66"},{$set:{age:6}},0,1)
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
{ "_id" : ObjectId("5cff2ddc5ba0016277aac7f4"), "name" : "user66", "age" : 6 }
{ "_id" : ObjectId("5cff2ea41434a33e16420347"), "name" : "user66", "age" : 6 }
# $inc 如果字段不存在则创建并赋值,已存在则数值累加
> db.c3.find()
{ "_id" : ObjectId("5cffe2871434a33e1642034a"), "name" : "user1", "score" : 1 }
{ "_id" : ObjectId("5cffe2911434a33e1642034b"), "name" : "user2", "score" : 5 }
{ "_id" : ObjectId("5cffe2981434a33e1642034c"), "name" : "user3" }
#第一个 {} 为空表示对所有数据进行操作
> db.c3.update({},{#inc:{score:10}},0,1)
> db.c3.find()
{ "_id" : ObjectId("5cffe2871434a33e1642034a"), "name" : "user1", "score" : 11 }
{ "_id" : ObjectId("5cffe2911434a33e1642034b"), "name" : "user2", "score" : 15 }
{ "_id" : ObjectId("5cffe2981434a33e1642034c"), "name" : "user3", "score" : 10 }
# 减3
> db.c3.update({name:"user2"},{$inc:{score:-3}},0,1)
> db.c3.find()
{ "_id" : ObjectId("5cffe2871434a33e1642034a"), "name" : "user1", "score" : 11 }
{ "_id" : ObjectId("5cffe2911434a33e1642034b"), "name" : "user2", "score" : 12 }
{ "_id" : ObjectId("5cffe2981434a33e1642034c"), "name" : "user3", "score" : 10 }
# $unset 删除字段
> db.c3.update({},{$unset:{score:1}},0,1)
> db.c3.find()
{ "_id" : ObjectId("5cffe2871434a33e1642034a"), "name" : "user1" }
{ "_id" : ObjectId("5cffe2911434a33e1642034b"), "name" : "user2" }
{ "_id" : ObjectId("5cffe2981434a33e1642034c"), "name" : "user3" }
# $push 往一个已经存在的数组追加值
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3 ] }
# 往数组追加一个值
> db.c4.update({name:"user1"},{$push:{arr:4}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 4 ] }
# $pop 从数组中弹出一个值,默认从最后一个,-1从第一个
# 从第一个弹出
> db.c4.update({name:"user1"},{$pop:{arr:-1}},0,1)
# 从最后一个弹出
> db.c4.update({name:"user1"},{$pop:{arr:1}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3 ] }
# $pushAll 往数组里添加多个值
> db.c4.update({name:"user1"},{$pushAll:{arr:[4,5,6,4,5,6]}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 4, 5, 6, 4, 5, 6 ] }
# $addToSet 往数组里添加数值,存在则添加失败,不存在则添加成功
> db.c4.update({name:"user1"},{$addToSet:{arr:3}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 4, 5, 6, 4, 5, 6 ] }
#添加多个值并且不重复 配合 $each 使用
> db.c4.update({name:"user1"},{$addToSet:{arr:{$each:[5,6,7]}}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 4, 5, 6, 4, 5, 6, 7 ] }
# $pull 删除一个指定值
> db.c4.update({name:"user1"},{$pull:{arr:4}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 5, 6, 5, 6, 7 ] }
# $pulAll 删除多个指定值
> db.c4.update({name:"user1"},{$pullAll:{arr:[5,6]}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 7 ] }
# $rename 修改字段的名字
> db.c4.update({name:"user1"},{$rename:{"arr":"post"}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "post" : [ 1, 2, 3, 7 ] }
# $ 符的使用,可以修改某段的值
> db.c4.update({"post.title":"linux"},{$set:{"post.$.cnt":"php lamp"}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "post" : [ 1, 2, 3, 7, { "title" : "linux", "cnt" : "php lamp" } ], "title" : [ "linux" ], "arr" : [ { "title" : "linux" } ] }
> db.c4.update({"post.title":"linux"},{$set:{"post.$.title":"php+mysql+ lamp"}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "post" : [ 1, 2, 3, 7, { "title" : "php+mysql+ lamp", "cnt" : "php lamp" } ], "title" : [ "linux" ], "arr" : [ { "title" : "linux" } ] }
#设置固定集合
#把普通集合转成固定集合,c1集合,设置大小10M,集合个数3个
db.runCommand({convertToCapped:"c1",size:10000000,max:3});
#查看集合状态
> db.c2.stats()
#检测解析影响行数
db.c2.find({name:"user5"}).explain({});
#以下参数说明
COLLSCAN
nReturned
返回数据个数
docsExamined
扫描数据行数
#设置索引(把 name 字段设置成索引)
> db.c2.ensureIndex({name:1})
#设置唯一索引
> db.c2.ensureIndex({age:1},{unique:1})
#查看索引个数
> db.c2.getIndexKeys()
[ { "_id" : 1 }, { "name" : 1 }, { "age" : 1 } ]
#查看索引
> db.c2.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.c2"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test.c2"
},
{
"v" : 1,
"unique" : true,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "test.c2"
}
]
#删除指定索引
> db.c2.dropIndex({age:1})
#删除所有索引
> db.c2.dropIndexes()
#数据导入导出
#导出数据 test 数据库 c2 集合 c2.out 导出的文件
mongoexport -d test -c c2 -o /tmp/c2.out
#导入数据
mongoimport -d test -c c2 /tmp/c2.out
#备份数据
mongodump -d test -o /tmp/mog/
#恢复备份数据
mongorestore -d test /tmp/mog/test/
#用户授权
#创建超级管理员用户(注:只能在admin库创建)
db.createUser(
{
user: "admin",
pwd: "123",
roles: [ { role: "root", db: "admin" } ]
}
)
#创建普通表管理员用户
db.createUser({user:"test",pwd:"123",roles:[{role:"dbAdmin",db:"test"}]})
#授权登录
mongo -uroot -p123 localhost:27017/admin
#授权使用
>db.auth("root","123")
创建用户
安装完之后,打开命令行,进入mongodb安装目录,在bin目录下执行 mongod 启动, 该模式是不需要安全认证的模式,
启动后,使用可视化工具如:Robo 3T 打开shell窗口
先创建一个有grant权限的用户,如root权限的用户:
use admin
db.createUser(
{
user: "admin",
pwd: "admin",
roles: [ { role: "root", db: "admin" } ]
}
)
user:用户名
pwd:密码
db:指定该用户的数据库,admin是用于权限控制的数据库,如果没有需要新建一个
roles:指定用户的角色,可以用一个空数组给新用户设定空角色;在roles字段,可以指定内置角色和用户定义的角色。role里的角色可以选:
Built-In Roles(内置角色):
1. 数据库用户角色:read、readWrite;
2. 数据库管理角色:dbAdmin、dbOwner、userAdmin;
3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
4. 备份恢复角色:backup、restore;
5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
6. 超级用户角色:root
// 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
7. 内部角色:__system
具体角色的功能:
Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限
主从复制 副本集
Master-Slave 复制(主从)
Replica Sets 复制(副本集)
#下载:
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz
#解压
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz
#移动文件
mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb
#启动mongodb
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/dblogs --fork
#编辑配置文件,使服务器重启后mongodb自动运行
vi /etc/rc.local
#将以下代码放置文件最后并保存退出
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/dblogs --fork
#查看mongodb进程
[root@localhost mongodb]# pstree -p |grep mongod
|-mongod(36708)-+-{mongod}(36709)
| |-{mongod}(36710)
| |-{mongod}(36711)
| |-{mongod}(36713)
| |-{mongod}(36714)
| |-{mongod}(36715)
| |-{mongod}(36716)
| |-{mongod}(36717)
| |-{mongod}(36718)
| `-{mongod}(36719)
#关闭mongodb
pkill mongodb
#配置环境变量/etc/profle文件。往里面新增一行:
export PATH=$PATH:/usr/local/mongodb/bin
其中$PATH后面跟的是实际安装的mongodb的bin所在目录
#接着重新加载配置:
source /etc/profile
#进入mongodb
mongo
#退出mongodb
exit
show dbs #查看数据库
db #查看当前数据库
show tables; #查看数据表(集合)
db.c1.insert({name:"user1"}); #往c1表里面插入一条json格式的数据
db.c1.insert({name:"user1",age:"30"}); #插入数据
db.c1.find(); #查看c1表里的数据
db.c1.remove() #删除c1集合里的所有数据
db.c1.update({name:"user1"},{name:"user22"}) #修改数据,其他数据会丢失
db.c1.update({name:"user1"},{$set:{name:"user22"}}) #修改数据
db.c1.find({条件})
db.c1.find({"name":"user1"});
db.c1.findOne({条件})
db.c1.findOne({"name":"user1"});
mongodb 数据类型
1、null
2、布尔类型
3、32位整数
4、64位整数
5、64位浮点数
6、字符串
7、符号
8、objectId类型
9、日期
10、正则表达式
11、代码
12、数组
13、内嵌文档
mogodb 基础操作 CURD
#删除当前数据库
db.dropDatabase();
#创建集合
db.createCollection("c1");
#删除集合
db.c1.drop();
#查看集合状态
> db.c2.stats()
#切换数据库
use dbname
1、insert (插入语句)
db.c1.insert({name:"user1"});
db.c1.insert({_id:1,name:"user1"}); #id相同插入失败
db.c1.save({name:"user1"});
db.c1.save({_id:1,name:"user1"}); #id相同会更新
2、remove #删除
db.c1.remove(); #删除所有数据
db.c1.remove({}); #删除所有数据
db.c1.remove({name:"user1"}); #删除指定数据
3、find() #查询
db.c1.find(); #查询所有数据
db.c1.find({name:"user1"}); #查询指定数据
db.c1.find({name:"user1",{name:1}); #查询指定数据显示指定字段,默认显示id,不要显示id{_id:0}
> db.c1.find({name:"user5"});
{ "_id" : ObjectId("5cfbd362517f0b0332b6a890"), "name" : "user5", "age" : "age5", "sex" : "sex5" }
> db.c1.find({name:"user5"},{name:1});
{ "_id" : ObjectId("5cfbd362517f0b0332b6a890"), "name" : "user5" }
> db.c1.find({name:"user5"},{name:1,_id:0});
{ "name" : "user5" }
> db.c1.find({name:"user5"},{name:1,age:1,_id:0});
{ "name" : "user5", "age" : "age5" }
条件表达式
#大于 $gt
> db.c1.find({num:{$gt:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#大于等于 $gte
> db.c1.find({num:{$gte:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#小于 $lt
> db.c1.find({num:{$lt:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
#小于等于 $lte
> db.c1.find({num:{$lte:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
#等于 :
> db.c1.find({num:5})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
#不等于 $ne
> db.c1.find({num:{$ne:5}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#查看有多少数据(统计数据)
> db.c1.count()
10
> db.c1.find().count()
10
#排序(升序) sort()
> db.c1.find().sort({num:1});
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#排序(降序) sort()
> db.c1.find().sort({num:-1});
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
#分页显示数据 skip(5)-表示从第5条开始显示,显示3条数据 limit(3)
> db.c1.find().skip(5).limit(3)
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
#分页并排序 sort
> db.c1.find().sort({num:-1}).skip(3).limit(3)
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
#判断数组中是否存在值 $all
> db.c2.find({post:{$all:[3,2]}})
{ "_id" : ObjectId("5cfbe5c9517f0b0332b6a8aa"), "name" : "user1", "post" : [ 1, 2, 3, 4, 5 ] }
#判断字段是否存在 $exists
> db.c2.find({name:{$exists:1}})
{ "_id" : ObjectId("5cfbe5c9517f0b0332b6a8aa"), "name" : "user1", "post" : [ 1, 2, 3, 4, 5 ] }
> db.c2.find({age:{$exists:1}})
>
#取余 取出余2,和余1的数据 $mod
> db.c1.find({num:{$mod:[2,1]}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
#$in 包含
> db.c1.find({num:{$in:[1,2,3]}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
#$nin 不包含
> db.c1.find({num:{$nin:[1,2,3,4,5,6]}})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#查询并且条件and(查询两个条件同时满足或者多个条件满足)
> db.c1.find({name:"user2",num:22})
{ "_id" : ObjectId("5cfd2bbbdc4a8ca381ed2710"), "name" : "user2", "age" : "age22", "num" : 22 }
#查询或者$or(查询条件范围内)
> db.c1.find({$or:[{name:"user2"},{name:"user3"},{name:"user5"}]})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a2"), "name" : "user3", "age" : "age3", "num" : 3 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a4"), "name" : "user5", "age" : "age5", "num" : 5 }
{ "_id" : ObjectId("5cfd2bbbdc4a8ca381ed2710"), "name" : "user2", "age" : "age22", "num" : 22 }
#查询$nor(查询条件范围外)
> db.c1.find({$nor:[{name:"user2"},{name:"user3"},{name:"user5"}]})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a0"), "name" : "user1", "age" : "age1", "num" : 1 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a3"), "name" : "user4", "age" : "age4", "num" : 4 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a5"), "name" : "user6", "age" : "age6", "num" : 6 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a6"), "name" : "user7", "age" : "age7", "num" : 7 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a7"), "name" : "user8", "age" : "age8", "num" : 8 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a8"), "name" : "user9", "age" : "age9", "num" : 9 }
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a9"), "name" : "user10", "age" : "age10", "num" : 10 }
#查询数组值的个数 $size
> db.c2.find({post:{$size:4}})
{ "_id" : ObjectId("5cfd2e9fdc4a8ca381ed2711"), "name" : "user2", "post" : [ 6, 7, 8, 9 ] }
{ "_id" : ObjectId("5cfd2eabdc4a8ca381ed2712"), "name" : "user2", "post" : [ 16, 17, 18, 19 ] }
> db.c2.find({post:{$size:2}})
{ "_id" : ObjectId("5cfd2f28dc4a8ca381ed2714"), "name" : "user2", "post" : [ 1, 12 ] }
#正则匹配查询
> db.c1.find({name:/user2/})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfd2bbbdc4a8ca381ed2710"), "name" : "user2", "age" : "age22", "num" : 22 }
> db.c1.find({name:/user2/i})
{ "_id" : ObjectId("5cfbddc2517f0b0332b6a8a1"), "name" : "user2", "age" : "age2", "num" : 2 }
{ "_id" : ObjectId("5cfd2bbbdc4a8ca381ed2710"), "name" : "user2", "age" : "age22", "num" : 22 }
{ "_id" : ObjectId("5cfd30d2dc4a8ca381ed2715"), "name" : "User2", "age" : "age11", "num" : 12 }
#查询取唯一值去掉重复值 distinct
> db.c1.distinct("name")
[
"user1",
"user2",
"user3",
"user4",
"user5",
"user6",
"user7",
"user8",
"user9",
"user10",
"User2"
]
> db.c1.distinct("num")
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 22, 12 ]
#查询多个数组里面的值
#方法一:
> db.c1.find({"post.titl":1})
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "titl" : 1 }, { "age" : 2 }, { "num" : 3 } ] }
方法二:
> db.c1.find({post:{$elemMatch:{titl:1}}})
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "titl" : 1 }, { "age" : 2 }, { "num" : 3 } ] }
#遍历游标,判断是否有数据,一条一条取出
> var x=db.c1.find()
> x.hasNext()
true
> x.next()
{
"_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"),
"name" : "user1",
"post" : [
{
"titl" : 1
},
{
"age" : 2
},
{
"num" : 3
}
]
}
>
#查询null的值
方法一:
> db.c2.find({age:{$type:10}});
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
方法二:
> db.c2.find({age:{$exists:1,$in:[null]}})
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
# $slice 针对数组使用有效
> db.c1.find({name:"user1"},{post:{$slice:1}})#取前1
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "titl" : 1 } ] }
{ "_id" : ObjectId("5cfdea782e9a2c8b9bb2f9de"), "name" : "user1", "post" : [ { "titl" : 12 } ] }
{ "_id" : ObjectId("5cfdeb432e9a2c8b9bb2f9df"), "name" : "user1", "post" : [ { "titl" : 1 } ] }
{ "_id" : ObjectId("5cfdebd12e9a2c8b9bb2f9e0"), "name" : "user1", "post" : [ { "titl" : 11 } ] }
> db.c1.find({name:"user1"},{post:{$slice:2}})#取前2
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "titl" : 1 }, { "age" : 2 } ] }
{ "_id" : ObjectId("5cfdea782e9a2c8b9bb2f9de"), "name" : "user1", "post" : [ { "titl" : 12 }, { "age" : 22 } ] }
{ "_id" : ObjectId("5cfdeb432e9a2c8b9bb2f9df"), "name" : "user1", "post" : [ { "titl" : 1 }, { "age" : 22 } ] }
{ "_id" : ObjectId("5cfdebd12e9a2c8b9bb2f9e0"), "name" : "user1", "post" : [ { "titl" : 11 }, { "age" : 21 } ] }
> db.c1.find({name:"user1"},{post:{$slice:-2}})#取后2
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "age" : 2 }, { "num" : 3 } ] }
{ "_id" : ObjectId("5cfdea782e9a2c8b9bb2f9de"), "name" : "user1", "post" : [ { "age" : 22 }, { "num" : 23 } ] }
{ "_id" : ObjectId("5cfdeb432e9a2c8b9bb2f9df"), "name" : "user1", "post" : [ { "age" : 22 }, { "num" : 3 } ] }
{ "_id" : ObjectId("5cfdebd12e9a2c8b9bb2f9e0"), "name" : "user1", "post" : [ { "age" : 21 }, { "num" : 3 } ] }
> db.c1.find({name:"user1"},{post:{$slice:[1,2]}})#跳过第1个,从第二个开始取
{ "_id" : ObjectId("5cfdea1c2e9a2c8b9bb2f9dd"), "name" : "user1", "post" : [ { "age" : 2 }, { "num" : 3 } ] }
{ "_id" : ObjectId("5cfdea782e9a2c8b9bb2f9de"), "name" : "user1", "post" : [ { "age" : 22 }, { "num" : 23 } ] }
{ "_id" : ObjectId("5cfdeb432e9a2c8b9bb2f9df"), "name" : "user1", "post" : [ { "age" : 22 }, { "num" : 3 } ] }
{ "_id" : ObjectId("5cfdebd12e9a2c8b9bb2f9e0"), "name" : "user1", "post" : [ { "age" : 21 }, { "num" : 3 } ] }
4、修改(update)
#把name:user3的这条数据的age值修改成21
> db.c2.update({name:"user3},{"$.age":"21"})
db.collection.update(criteria,objNew,upsert,multi)
参数说明:
criteria:用于设置查询条件的对象
objnew:用于设置更新内容的对象
upsert:如果记录已经存在,更新它否则新增一个记录(0或1)
multi:如果有多个符合条件的记录,全部更新(0或1)
注意:默认情况下,只会更新第一个符合条件的记录
实例操作:
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
#对象不存在新增一个,存在则修改
> db.c2.update({name:"user66"},{name:"user6"},1)
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
{ "_id" : ObjectId("5cff2ddc5ba0016277aac7f4"), "name" : "user66" }
#对象不存在修改失败,存在则修改
> db.c2.update({name:"user6"},{name:"user66"},0)
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
{ "_id" : ObjectId("5cff2ddc5ba0016277aac7f4"), "name" : "user66" }
#更新所有配置出的对象,
# $set 魔术变量,第四个参数必需存在才能使用
> db.c2.update({name:"user6"},{$set:{name:"user66"}},0,1)
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
{ "_id" : ObjectId("5cff2ddc5ba0016277aac7f4"), "name" : "user66" }
# $set 更新的对象存在则更新,不存在则新一个对象
> db.c2.update({name:"user66"},{$set:{age:6}},0,1)
> db.c2.find()
{ "_id" : ObjectId("5cfe16752e9a2c8b9bb2f9e1"), "name" : "user1", "age" : null }
{ "_id" : ObjectId("5cfe167a2e9a2c8b9bb2f9e2"), "name" : "user2", "age" : null }
{ "_id" : ObjectId("5cfe16832e9a2c8b9bb2f9e3"), "name" : "user3", "age" : 22 }
{ "_id" : ObjectId("5cfe16892e9a2c8b9bb2f9e4"), "name" : "user4", "age" : 22 }
{ "_id" : ObjectId("5cfe168f2e9a2c8b9bb2f9e5"), "name" : "user5", "age" : 25 }
{ "_id" : ObjectId("5cff2ddc5ba0016277aac7f4"), "name" : "user66", "age" : 6 }
{ "_id" : ObjectId("5cff2ea41434a33e16420347"), "name" : "user66", "age" : 6 }
# $inc 如果字段不存在则创建并赋值,已存在则数值累加
> db.c3.find()
{ "_id" : ObjectId("5cffe2871434a33e1642034a"), "name" : "user1", "score" : 1 }
{ "_id" : ObjectId("5cffe2911434a33e1642034b"), "name" : "user2", "score" : 5 }
{ "_id" : ObjectId("5cffe2981434a33e1642034c"), "name" : "user3" }
#第一个 {} 为空表示对所有数据进行操作
> db.c3.update({},{#inc:{score:10}},0,1)
> db.c3.find()
{ "_id" : ObjectId("5cffe2871434a33e1642034a"), "name" : "user1", "score" : 11 }
{ "_id" : ObjectId("5cffe2911434a33e1642034b"), "name" : "user2", "score" : 15 }
{ "_id" : ObjectId("5cffe2981434a33e1642034c"), "name" : "user3", "score" : 10 }
# 减3
> db.c3.update({name:"user2"},{$inc:{score:-3}},0,1)
> db.c3.find()
{ "_id" : ObjectId("5cffe2871434a33e1642034a"), "name" : "user1", "score" : 11 }
{ "_id" : ObjectId("5cffe2911434a33e1642034b"), "name" : "user2", "score" : 12 }
{ "_id" : ObjectId("5cffe2981434a33e1642034c"), "name" : "user3", "score" : 10 }
# $unset 删除字段
> db.c3.update({},{$unset:{score:1}},0,1)
> db.c3.find()
{ "_id" : ObjectId("5cffe2871434a33e1642034a"), "name" : "user1" }
{ "_id" : ObjectId("5cffe2911434a33e1642034b"), "name" : "user2" }
{ "_id" : ObjectId("5cffe2981434a33e1642034c"), "name" : "user3" }
# $push 往一个已经存在的数组追加值
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3 ] }
# 往数组追加一个值
> db.c4.update({name:"user1"},{$push:{arr:4}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 4 ] }
# $pop 从数组中弹出一个值,默认从最后一个,-1从第一个
# 从第一个弹出
> db.c4.update({name:"user1"},{$pop:{arr:-1}},0,1)
# 从最后一个弹出
> db.c4.update({name:"user1"},{$pop:{arr:1}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3 ] }
# $pushAll 往数组里添加多个值
> db.c4.update({name:"user1"},{$pushAll:{arr:[4,5,6,4,5,6]}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 4, 5, 6, 4, 5, 6 ] }
# $addToSet 往数组里添加数值,存在则添加失败,不存在则添加成功
> db.c4.update({name:"user1"},{$addToSet:{arr:3}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 4, 5, 6, 4, 5, 6 ] }
#添加多个值并且不重复 配合 $each 使用
> db.c4.update({name:"user1"},{$addToSet:{arr:{$each:[5,6,7]}}},0,1)
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 4, 5, 6, 4, 5, 6, 7 ] }
# $pull 删除一个指定值
> db.c4.update({name:"user1"},{$pull:{arr:4}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 5, 6, 5, 6, 7 ] }
# $pulAll 删除多个指定值
> db.c4.update({name:"user1"},{$pullAll:{arr:[5,6]}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "arr" : [ 1, 2, 3, 7 ] }
# $rename 修改字段的名字
> db.c4.update({name:"user1"},{$rename:{"arr":"post"}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "post" : [ 1, 2, 3, 7 ] }
# $ 符的使用,可以修改某段的值
> db.c4.update({"post.title":"linux"},{$set:{"post.$.cnt":"php lamp"}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "post" : [ 1, 2, 3, 7, { "title" : "linux", "cnt" : "php lamp" } ], "title" : [ "linux" ], "arr" : [ { "title" : "linux" } ] }
> db.c4.update({"post.title":"linux"},{$set:{"post.$.title":"php+mysql+ lamp"}})
> db.c4.find()
{ "_id" : ObjectId("5cffe61f1434a33e1642034d"), "name" : "user1", "post" : [ 1, 2, 3, 7, { "title" : "php+mysql+ lamp", "cnt" : "php lamp" } ], "title" : [ "linux" ], "arr" : [ { "title" : "linux" } ] }
#设置固定集合
#把普通集合转成固定集合,c1集合,设置大小10M,集合个数3个
db.runCommand({convertToCapped:"c1",size:10000000,max:3});
#查看集合状态
> db.c2.stats()
#检测解析影响行数
db.c2.find({name:"user5"}).explain({});
#以下参数说明
COLLSCAN
nReturned
返回数据个数
docsExamined
扫描数据行数
#设置索引(把 name 字段设置成索引)
> db.c2.ensureIndex({name:1})
#设置唯一索引
> db.c2.ensureIndex({age:1},{unique:1})
#查看索引个数
> db.c2.getIndexKeys()
[ { "_id" : 1 }, { "name" : 1 }, { "age" : 1 } ]
#查看索引
> db.c2.getIndexes()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.c2"
},
{
"v" : 1,
"key" : {
"name" : 1
},
"name" : "name_1",
"ns" : "test.c2"
},
{
"v" : 1,
"unique" : true,
"key" : {
"age" : 1
},
"name" : "age_1",
"ns" : "test.c2"
}
]
#删除指定索引
> db.c2.dropIndex({age:1})
#删除所有索引
> db.c2.dropIndexes()
#数据导入导出
#导出数据 test 数据库 c2 集合 c2.out 导出的文件
mongoexport -d test -c c2 -o /tmp/c2.out
#导入数据
mongoimport -d test -c c2 /tmp/c2.out
#备份数据
mongodump -d test -o /tmp/mog/
#恢复备份数据
mongorestore -d test /tmp/mog/test/
#用户授权
#创建超级管理员用户(注:只能在admin库创建)
db.createUser(
{
user: "admin",
pwd: "123",
roles: [ { role: "root", db: "admin" } ]
}
)
#创建普通表管理员用户
db.createUser({user:"test",pwd:"123",roles:[{role:"dbAdmin",db:"test"}]})
#授权登录
mongo -uroot -p123 localhost:27017/admin
#授权使用
>db.auth("root","123")
创建用户
安装完之后,打开命令行,进入mongodb安装目录,在bin目录下执行 mongod 启动, 该模式是不需要安全认证的模式,
启动后,使用可视化工具如:Robo 3T 打开shell窗口
先创建一个有grant权限的用户,如root权限的用户:
use admin
db.createUser(
{
user: "admin",
pwd: "admin",
roles: [ { role: "root", db: "admin" } ]
}
)
user:用户名
pwd:密码
db:指定该用户的数据库,admin是用于权限控制的数据库,如果没有需要新建一个
roles:指定用户的角色,可以用一个空数组给新用户设定空角色;在roles字段,可以指定内置角色和用户定义的角色。role里的角色可以选:
Built-In Roles(内置角色):
1. 数据库用户角色:read、readWrite;
2. 数据库管理角色:dbAdmin、dbOwner、userAdmin;
3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
4. 备份恢复角色:backup、restore;
5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
6. 超级用户角色:root
// 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)
7. 内部角色:__system
具体角色的功能:
Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限
主从复制 副本集
Master-Slave 复制(主从)
Replica Sets 复制(副本集)
发表评论 取消回复