mongodb操作数据库集合命令备份与恢复新建用户
目录
mongodb操作数据库/集合命令/备份与恢复/新建用户
操作数据库/集合命令/备份与恢复/新建用户
mongodb操作数据库/集合命令
增删改查
数据库常用命令(操作集合\数据库)
help//帮助
db.help()//集合所有方法
db.stats()//集合当前状态信息
db//当前数据库db.getName()
show dbs//所有数据库列表
use test//切换数据库
db.createCollection('collection1')//数据库创建集合/表
db.getCollectionNames()//获取数据库所有集合/表
db.dropDatabase()//删除当前数据库
show tables 或者 show collections//查看当前库中的表/集合
db.mycollection.drop()//删除mycollection集合
集合-Other(操作文档)
db.getCollection('集合名').getIndexes()//查看集合索引
集合-查(操作文档)
db.collection1.find()//查所有
db.getCollection("collection1").find()
db.collection1.find({'字段名':'字段属性'})//查指定的文档
db.getCollection("collection1").find({'字段名':'字段属性'})
db.collection1.find({'expect':{$exists:false}})//查expect字段不存在的文档
db.getCollection('collection1').find({'expect':{$exists:false}})
db.collection1.find({'字段1':{$exists:false},'字段2':{$exists:true}})//多字段查询
db.getCollection('collection1').find({'字段1':{$exists:false},'字段2':{$exists:true}})
db.collection1.find({post_text:{$regex:"runoob"}})//查post_text部分匹配runoob的文档
db.getCollection("collection1").find({post_text:{$regex:"runoob"}})
db.collection1.find({'people.name':‘kj’})//name是people的⼦字段。查找所有name为kj的⽂档
db.getCollection('collection1').find({'people.name':‘kj’})
db.collection1.find().count()//查数量
db.getCollection("collection1").find().count()
db.collection1.find().sort({})//先排序
db.getCollection("collection1").find().sort({})
db.collection1.find().limit(3).skip(3)//跳过3条查前3条
db.getCollection("collection1").find().limit(3).skip(3)
db.collection1.find({$or:[{age:21},{age:22}]})//$or二选一
db.getCollection("collection1").find({$or:[{age:21},{age:22}]})
db.collection1.find().findOne()//查第一条
db.getCollection("collection1").find().findOne()
db.collection1.find({name:'kj4'})//查kj4
db.getCollection("collection1").find({name:'kj4'})
db.collection1.find({age:{$lt:20}})//查小于20
db.getCollection("collection1").find({age:{$lt:20}})
db.collection1.find({age:{$gt:20}})//查大于20
db.getCollection("collection1").find({age:{$gt:20}})
db.collection1.find({age:{$lte:20}})//查小于等于20
db.getCollection("collection1").find({age:{$lte:20}})
db.collection1.find({age:{$gte:20}})//查大于等于20
db.getCollection("collection1").find({age:{$gte:20}})
db.collection1.find({age:{$gte:20,$lte:20}})//查大于等于20且小于等于20
db.getCollection("collection1").find({age:{$gte:20,$lte:20}})
db.collection1.find({name:/1/})//name中含有1
db.getCollection("collection1").find({name:/1/})
db.collection1.find({name:/^1/})//name中开头含有1
db.getCollection("collection1").find({name:/^1/})
db.collection1.find({name:/1$/})//name中结尾含有1
db.getCollection("collection1").find({name:/1$/})
db.collection1.find({},{name:1,age:0})//指定列数据1为显示该列数据,0为不显示该列数据
db.getCollection("collection1").find({},{name:1,age:0})
我们如果需要查询同时满足两个以上条件,需要使用$and操作符将条件进行关联。(相 当于SQL的and)
如果两个以上条件之间是或者的关系,我们使用 $or 操作符进行关联
db.collection1.find({$and:[{age:{$gte:NumberInt(10)}},{age:{$lt:NumberInt(50)}}]})//查询collection1集合中age大于等于10并且小于50的文档
db.comment.find({$or:[ {userid:"1003"} ,{likenum:{$lt:1000} }]})//查询评论集合中userid为1003,或者点赞数小于1000的文档记录
集合-删(操作文档)
db.collection1.remove({name:'kj4'})
db.getCollection('API').remove({'created':{'\$gt':154, '$lt':156}}), 为删除集合“API”中‘created’字段属性在154-156之间的数据。
集合-增(操作文档)
db.collection1.insert([{name:'kj2',age:21},{age:21,name:'kj3'}])
db.collection1.save({name:'kj3',age:21})
db.runoob.insert({"name":"菜鸟就是我"})//数据库在runoob表中插入数据
集合-改(操作文档)
db.collection1.update({name:'kj3'},{$set{age:22}})
重新设置age值
db.collection1.update({name:'kj3'},{$inc{age:23}})
在age的基础上加等于
db.collection1.update({name:'kj4'},{$inc:{age:23}},true,true)
修改两条相等的name=kj4
如果没有name=kj4 true是创建false为不创建
MongoDB 数据库备份与恢复
mongoexport备份某个表语法格式:mongoexport --port 端口号 -d 库名 -c 表名 -o 备份文件路径.json
mongoexport备份某个表csv格式:mongoexport --port 端口号 -d 库名 -c 表名 --type=csv -f 备份的字段 -o 备份文件路径.json
mongoimport还原某个表json格式:mongoimport --port 26017 -d 要还原的库名-c 表名 备份文件路径.json
mongoimport还原某个表csv格式:mongoimport --port 26017 -d 库名 -c 表名–type=csv --headerline 备份文件路径.csv
mongodump备份库:mongodump --port 26017 -d 库名 -o 备份文件路径
mongorestore还原:mongorestore --port 26017 -d 库名 备份文件路径 --drop
压缩格式备份mongodump --port 26017 -d zabbix -o /data/backup/zabbix_db --gzip
将mysql中的表导出还原到mongodb
1\导出mysql中的表为csv格式
2\将Navicat导出包含列标题的csv传到Linux中
3\mongoimport --port 26017 -d zabbix -c users --type=csv --headerline /tmp/users.csv
mongodb设置用户名和密码
> show dbs
# 在mongodb新版本里并没有admin数据库,但是并不妨碍操作。
# 进入admin数据库
> use admin
# 创建管理员账户
> db.createUser(
{
user: "创建用户名",
pwd: "密码",
roles: [ { role: "角色,如:dbAdmin", db: "admin" } ]
}
);
# mongodb中的用户是基于身份role的,该管理员账户的role是 root 代表超级管理员权限
# 验证用户添加是否成功 如果返回1,则表示成功。
> db.auth("useradmin", "adminpassword")
# 退出系统
> exit
db.auth()方法理解为 用户的验证功能
# 修改配置
sudo vi /etc/mongod.conf
auth=true
# 重启
mongodb sudo service mongod restart
#进入mongodb 管理员账户登录,用该账户创建其他数据库管理员账号
> use admin
> db.auth("root", "root")
# 新建你需要管理的mongodb 数据的账号密码。
MongoDB数据库角色
内建的角色
数据库用户角色:read、readWrite;
数据库管理角色:dbAdmin、dbOwner、userAdmin;
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
备份恢复角色:backup、restore;
所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、 dbAdminAnyDatabase
超级用户角色:root // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、 userAdminAnyDatabase)
内部角色:__system
角色说明:
Read:允许用户读取指定数据库
readWrite:允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,赋予用户所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,赋予用户所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限