mongodb 使用小结

install

基于centos
sudo yum install -y mongodb-org

如果 找不到 repo、执行一下操作

vi /etc/yum.repos.d/mongodb-org-3.4.repo


[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

User Management

db.createUser(user, writeConcern)

{ user: "<name>",
  pwd: "<cleartext password>",
  customData: { <any information> },
  roles: [
    { role: "<role>", db: "<database>" } | "<role>",
    ...
  ]
}

role

  • read

  • readWrite

  • dbAdmin

  • dbOwner

  • userAdmin

db.removeUser(username)

db.grantRolesToUser(username, roles, writeConcern)

db.revokeRolesFromUser()

db.getUser(username, args)

db.getUsers()

db.auth( , )

System Collections

admin.system.roles

admin.system.users

admin.system.version

.system.namespaces

.system.indexes

.system.profile

.system.js

Connection String URI Format

mongodb://[username:password@]host1[:port1][,host2[:port2],…[,hostN[:portN]]][/[database][?options]]

Database Methods

db.dropDatabase()

Removes the current database.

db.cloneDatabase(“hostname”)

Copies a remote database to the current database. The command assumes that the remote database has the same name as the current database.