MongoDB

鉴于其安全性很差,一定要修改端口,设置密码

  1. 资料

centos

  1. centos7安装
  2. CentOS7上Mongodb安装、卸载

Ubuntu

  1. 资料
    • 安装
    • mongod是服务端,mongo是客户端
    • 默认配置文件/etc/mongod.conf,默认使用的数据库文件路径是/data/db/。
  2. 命令
    • 运行服务: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() // 服务的版本
wangyaqi.cn all right reserved,powered by Gitbook该文件修订时间: 2020-01-29 16:06:14

results matching ""

    No results matching ""

    results matching ""

      No results matching ""