Searching...
Monday, 10 September 2012

MongoDB Operations

10:56

Help Command

help command provides top level help, see below examples

C:\mongodb\bin\mongo
MongoDB shell version: 2.0.6
connecting to: test
> help

db.help()= help on db methods 
db.mycoll.help() = help on collection methods 
rs.help() = help on replica set methods 
help admin = administrative help 
help connect = connecting to a db help 
help keys = key shortcuts 
help misc = misc things to know 
help mr = mapreduce 
show dbs = show database names 
show collections = show collections in current database 
show users = show users in current database 
show profile = show most recent system.profile
entries with time >= 1ms 
show logs = show the accessible logger names 
show log [name] = prints out the last segment of
log in memory, 'global' is default 
use = set current database 
db.foo.find() = list objects in collection foo 
db.foo.find( { a : 1 }) = list objects in foo where a == 1 
it = result of the last line evaluated; use to further iterate 
DBQuery.shellBatchSize = x = set default number of
items to display on shell 
exit = quit the mongo shell

db.help()- Db level commands

 

> db.help()
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.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.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().
db.mycollection.help() – This will helps on collection(Table level) methods.

DBCollection help

db.mycollection.find().help() - show DBCursor help 
db.mycollection.count() 
db.mycollection.dataSize() 
db.mycollection.distinct( key ) - eg. db.mycollection.distinct( 'x' ) 
db.mycollection.drop() drop the collection 
db.mycollection.dropIndex(name) 
db.mycollection.dropIndexes() 
db.mycollection.ensureIndex() - options is an object
with these possible fields: name, unique, dropDups 
db.mycollection.reIndex() 
db.mycollection.find([query],[fields]) - query is an optional
query filter. fields is optional set of fields to return.
e.g. db.mycollection.find( {x:77} , {name:1, x:1} ) 
db.mycollection.find(...).count() 
db.mycollection.find(...).limit(n) 
db.mycollection.find(...).skip(n) 
db.mycollection.find(...).sort(...) 
db.mycollection.findOne([query]) 
db.mycollection.findAndModify( { update : ... ,
remove : bool [, query: {}, sort: {}, 'new': false] } ) 
db.mycollection.getDB() get DB object associated with collection 
db.mycollection.getIndexes() 
db.mycollection.group( { key : ..., initial: ...,
reduce : ...[, cond: ...] } ) 
db.mycollection.mapReduce( mapFunction , reduceFunction , ;
optional params> ) 
db.mycollection.remove(query) 
db.mycollection.renameCollection( newName , dropTarget> )
renames the collection. 
db.mycollection.runCommand( name) runs a db command with
the given name where the first param is the collection name 
db.mycollection.save(obj) 
db.mycollection.stats() 
db.mycollection.storageSize() - includes free space
allocated to this collection 
db.mycollection.totalIndexSize() - size in bytes of all the indexes 
db.mycollection.totalSize() - storage allocated for all data and indexes    db.mycollection.update(query, object[, upsert_bool, multi_bool]) 
db.mycollection.validate() - SLOW 
db.mycollection.getShardVersion() - only for use with sharding 
db.mycollection.getShardDistribution() -
prints statistics about data distribution in the cluster

 

db.mycollection.find().help() 

This will provide Cursor level help.

find() modifiers
.sort( {...} ) 
.limit( n ) 
.skip( n ) 
.count() - total # of objects matching query, ignores skip,limit 
.size() - total # of objects cursor would return, honors skip,limit 
.explain([verbose]) 
.hint(...) 
.showDiskLoc() - adds a $diskLoc field to each returned object

Cursor Methods

.forEach( func ) 
.map( func ) 
.hasNext() 
.next()


 
 

0 comments: