NoSql

mongdb 쉘 활용 팁

우혁이 아빠 2011. 6. 25. 15:30


> show dbs
admin   (empty)
foobar  0.03125GB
local   (empty)
test    0.03125GB

> show collections
blog
system.indexes

> show users

> show profile
db.system.profile is empty
Use db.setProfilingLevel(2) will enable profiling
Use db.system.profile.find() to show raw profile entries

> 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 nonmas
ter member of a replica pair
        db.getName()
        db.getPrevError()
        db.getProfilingLevel() - deprecated
        db.getProfilingStatus() - returns if profiling is on and slow threshold

        db.getReplicationInfo()
        db.getSiblingDB(name) get the db at the same server as this one
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation in the db
        db.listCommands() lists all the db commands
        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, tu
rns 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.foo.help()
DBCollection help
        db.foo.find().help() - show DBCursor help
        db.foo.count()
        db.foo.dataSize()
        db.foo.distinct( key ) - eg. db.foo.distinct( 'x' )
        db.foo.drop() drop the collection
        db.foo.dropIndex(name)
        db.foo.dropIndexes()
        db.foo.ensureIndex(keypattern[,options]) - options is an object with the
se possible fields: name, unique, dropDups
        db.foo.reIndex()
        db.foo.find([query],[fields]) - query is an optional query filter. field
s is optional set of fields to return.
                                                      e.g. db.foo.find( {x:77} ,
 {name:1, x:1} )
        db.foo.find(...).count()
        db.foo.find(...).limit(n)
        db.foo.find(...).skip(n)
        db.foo.find(...).sort(...)
        db.foo.findOne([query])
        db.foo.findAndModify( { update : ... , remove : bool [, query: {}, sort:
 {}, 'new': false] } )
        db.foo.getDB() get DB object associated with collection
        db.foo.getIndexes()
        db.foo.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
        db.foo.mapReduce( mapFunction , reduceFunction , <optional params> )
        db.foo.remove(query)
        db.foo.renameCollection( newName , <dropTarget> ) renames the collection
.
        db.foo.runCommand( name , <options> ) runs a db command with the given n
ame where the first param is the collection name
        db.foo.save(obj)
        db.foo.stats()
        db.foo.storageSize() - includes free space allocated to this collection
        db.foo.totalIndexSize() - size in bytes of all the indexes
        db.foo.totalSize() - storage allocated for all data and indexes
        db.foo.update(query, object[, upsert_bool, multi_bool])
        db.foo.validate() - SLOW
        db.foo.getShardVersion() - only for use with sharding

함수가 어떤 기능을 수행하는지 알기 위해서는 괄호 없이 입력하면 된다.
> db.foo.update
function (query, obj, upsert, multi) {
    assert(query, "need a query");
    assert(obj, "need an object");
    var firstKey = null;
    for (var k in obj) {
        firstKey = k;
        break;
    }
    if (firstKey != null && firstKey[0] == "$") {
        this._validateObject(obj);
    } else {
        this._validateForStorage(obj);
    }
    this._mongo.update(this._fullName, query, obj, upsert ? true : false, multi
? true : false);
}

http://www.mongodb.org/display/DOCS/Manual ---> 메뉴얼 페이지