mongodb基础命令大集合

mongodb基础命令大集合

Basic Commands

User management commands:

db.createUser()
db.dropUser()

Collection management commands:

db.<collection>.renameCollection()
db.<collection>.createIndex()
db.<collection>.drop()

Database management commands:

db.dropDatabase()
db.createCollection()

Database status command:

db.serverStatus()

Creating index with Database Command:

db.runCommand(
  { "createIndexes": <collection> },
  { "indexes": [
    {
      "key": { "product": 1 }
    },
    { "name": "name_index" }
    ]
  }
)

Creating index with Shell Helper:

db.<collection>.createIndex(
  { "product": 1 },
  { "name": "name_index" }
)

Introspect a Shell Helper:

db.<collection>.createIndex

Logging Basics

Get the logging components:

mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
  db.getLogComponents()
'

Change the logging level:

mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
  db.setLogLevel(0, "index")
'

Tail the log file:

tail -f /data/db/mongod.log

Update a document:

mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
  db.products.update( { "sku" : 6902667 }, { $set : { "salePrice" : 39.99} } )
'

Look for instructions in the log file with grep:

grep -R 'update' /data/db/mongod.log

Profilling the Database

Get profiling level:

mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
  use newDB
  db.getProfilingLevel()
'

Set profiling level:

mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
  db.setProfilingLevel(1)
'

Show collections:

mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
show collections
'

Set slowms to 0:

mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
  db.setProfilingLevel( 1, { slowms: 0 } )
'

Insert one document into a new collection:

mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
  db.new_collection.insert( { "a":1 } )
'

Get profiling data from system.profile:

mongo admin --host 192.168.103.100:27000 -u m103-admin -p m103-pass --eval '
  db.system.profile.find().pretty()

Basic Security

Print configuration file:

cat /etc/mongod.conf

Launch standalone mongod:

mongod -f /etc/mongod.conf

Connect to mongod:

mongo --host 127.0.0.1:27017

Create new user with the root role (also, named root):

use admin
db.createUser({
  user: "root",
  pwd: "root123",
  roles : [ "root" ]
})

Connect to mongod and authenticate as root:

mongo --username root --password root123 --authenticationDatabase admin

Run DB stats:

db.stats()

Shutdown the server:

use admin
db.shutdownServer()

Built-In Roles

Authenticate as root user:

mongo admin -u root -p root123

Create security officer:

db.createUser(
  { user: "security_officer",
    pwd: "h3ll0th3r3",
    roles: [ { db: "admin", role: "userAdmin" } ]
  }
)

Create database administrator:

db.createUser(
  { user: "dba",
    pwd: "c1lynd3rs",
    roles: [ { db: "admin", role: "userAdmin" } ]
  }
)

Grant role to user:

db.grantRolesToUser( "dba",  [ { db: "playground", role: "dbOwner"  } ] )

Show role privileges:

db.runCommand( { rolesInfo: { role: "dbOwner", db: "playground" }, showPrivileges: true} )

Server Tools Overview

List mongodb binaries:

find /usr/bin/ -name "mongo*"

Create new dbpath and launch mongod:

mkdir -p ~/first_mongod
mongod --port 30000 --dbpath ~/first_mongod --logpath ~/first_mongod/mongodb.log --fork

Use mongostat to get stats on a running mongod process:

mongostat --help
mongostat --port 30000

Use mongodump to get a BSON dump of a MongoDB collection:

mongodump --help
mongodump --port 30000 --db applicationData --collection products
ls dump/applicationData/
cat dump/applicationData/products.metadata.json

Use mongorestore to restore a MongoDB collection from a BSON dump:

mongorestore --drop --port 30000 dump/

Use mongoexport to export a MongoDB collection to JSON or CSV (or stdout!):

mongoexport --help
mongoexport --port 30000 --db applicationData --collection products
mongoexport --port 30000 --db applicationData --collection products -o products.json

Tail the exported JSON file:

tail products.json

Use mongoimport to create a MongoDB collection from a JSON or CSV file:

mongoimport --port 30000 products.json
阅读更多

扫码向博主提问

aladdin_sun

战个痛
去开通我的Chat快问
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页