MongoDB
鉴于其安全性很差,一定要修改端口,设置密码
- 资料
- 官方手册
- MongoDB基本管理命令
- MongoDB 创建数据库
- Mongodb3.0 配置身份验证db.createUser
- 配置内存缓存大小
- MongoDB数据库中关于roles权限的说明
- 超级管理员权限
use admin db.createUser( { user: "adm", pwd: "pwd", roles: [ "root"] } )
- 单库读写权限
use db2 db.createUser( { user: "db2RW", pwd: "pwd", roles: [ { role: "readWrite", db: "db2" }]})
- 多库权限
use admin db.createUser( { user: "andrew", pwd: "pwd", roles: [ { role: "readWrite", db: "db1" } , { role: "readWrite", db: "db2" }]})
- 超级管理员权限
centos
Ubuntu
- 资料
- 安装
- mongod是服务端,mongo是客户端
- 默认配置文件/etc/mongod.conf,默认使用的数据库文件路径是/data/db/。
- 命令
- 运行服务:mongod --dbpath=/data/db --fork --logpath=/data/db/mongod.log
- 关闭服务
启动mongo客户端: mongo mongo --port 12345 // 本机访问 mongo --port 12345 -u account -p pwd --authenticationDatabase database_name 输入: use admin db.shutdownServer()
- 检查是否启动,lsof -i :27017
- 远程访问配置:mongodb.conf
- 屏蔽bind_ip = 127.0.0.1
- 启用auth = true
- 删除用户 : db.dropUser("dbuser")
客户端命令
查询
db.system.users.findOne({user:'andrew'})
db.system.users.find({})
db.{collection}.find({})
db.{collection}.count({}) // 数量
db.{collection}.find({"key" : "value"}).sort({createDate:-1}) // 倒序
db.{collection}.find({"key" : {"$ne":null} }) // 字段不为空
db.{collection}.find({"key" : {"$not":/keyword/} }) // 字段不包括关键字(模糊查询)
db.collection.find({ "key" : { $gt: value } } ); // 大于 : field值 > value
db.collection.find({ "key" : { $lt: value } } ); // 小于 : field值 < value
db.{collection}.find({ "key": {'$not':/keyword/, "$ne":null} }) // 字段多条件查询
db.{collection}.find({"key" : {$regex:/value/} }) // 模糊查询
db.{collection}.find({"key" : {$regex:/value.*/i} }) // 模糊查询
db.{collection}.find({"key.childKey": "value"}) // 子对象数组查询
db.{collection}.find({key: { $elemMatch: { childKey: "value"}} }) // 子对象数组查询
更新
db.{collection}.update({}, {$set:{"key":null}}, false, true) // 更新字段值成null
删除
db.{collection}.remove({}) // 删除所有内容,不包括collection
db.{collection}.remove({"key" : "value"}) // 删除指定
collection
db.{collection}.drop() // 删除collection
字段操作
db.{collection}.update({}, {$set: {'col1': null}}, {multi: 1}) // 新增字段 db.{collection}.update({}, {$rename: {'oldColumn': 'newColumn'}, {multi: 1}}) // 字段改名 db.{collection}.update({}, {$unset: {'col1': null}}, false, true) // 删除字段
其他
index
insertMany:https://blog.csdn.net/huangshulang1234/article/details/79030567?utm_source=blogxgwz0。快了十倍
db.version() // 服务的版本