ドットインストール MongoDBの基礎 #4 - 各種コマンド


より。

データベースの一覧を見る

> show dbs;
local   (empty)

データベースを切り替える

> use local;
switched to db local
> use blog_app;
switched to db blog_app
> show dbs;
local   (empty)
> db.createCollection('posts');
{ "ok" : 1 }
> show dbs;
blog_app        0.203125GB
local   (empty)

ヘルプを見る

> db.help();
DB methods:
        db.addUser(username, password[, readOnly=false])
        db.auth(username, password)
        db.cloneDatabase(fromhost)
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost)
        db.createCollection(name, { size : ..., capped : ..., max : ... } )
        db.currentOp() displays the current operation in the db
        db.dropDatabase()
        db.eval(func, args) run code server-side
        db.getCollection(cname) same as db['cname'] or db.cname
        db.getCollectionNames()
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow this connection to read from the nonmaster member of a replica pair
        db.getName()
        db.getPrevError()
        db.getProfilin        db.getProfilin        db.getProfilingStatus() - returns if profiling is on and slow threshold
        db.getReplicationInfo()
        db.getSiblingDB(name) get the db at the         db.getSiblingDB(na      db.isMaster() check replica primary sta        db.getSiblingDB(name) get the db at the    tion in the db
        db.listCommands() lists all the db commands
        db.logout()
        db.printCollectionStats()
        db.printReplicationInfo()
        db.printSlaveReplicationInfo()
        db.printShardingStatus()
        db.removeUser(username)
        db.repairDatabase()
        db.resetError()
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
        db.serverStatus()
        db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all
        db.shutdownServer()
        db.stats()
        db.version() current version of the server
        db.getMongo().setSlaveOk() allow queries on a replication slave server
        db.fsyncLock() flush data to disk and lock server for backups
        db.fsyncUnock() unlocks server following a db.fsyncLock()

screenのC-a C-[とpbcopyでちょっと失敗したかも……

データベースの情報を見る

> db.stats();
{
        "db" : "blog_app",
        "collections" : 3,
        "objects" : 4,
        "avgObjSize" : 58,
        "dataSize" : 232,
        "storageSize" : 20480,
        "numExtents" : 3,
        "indexes" : 1,
        "indexSize" : 8176,
        "fileSize" : 201326592,
        "nsSize" : 16,
        "ok" : 1
}

データベースを消す

> db.dropDatabase();
{ "dropped" : "blog_app", "ok" : 1 }
> show dbs;
local   (empty)


前にいじったはずなのに全然覚えてなかった……