// db.js if ( typeof DB == "undefined" ){ DB = function( mongo , name ){ this._mongo = mongo; this._name = name; } } DB.prototype.getMongo = function(){ assert( this._mongo , "why no mongo!" ); return this._mongo; } DB.prototype.getSisterDB = function( name ){ return this.getMongo().getDB( name ); } DB.prototype.getName = function(){ return this._name; } DB.prototype.getCollection = function( name ){ return new DBCollection( this._mongo , this , name , this._name + "." + name ); } DB.prototype.commandHelp = function( name ){ var c = {}; c[name] = 1; c.help = true; return this.runCommand( c ).help; } DB.prototype.runCommand = function( obj ){ if ( typeof( obj ) == "string" ){ var n = {}; n[obj] = 1; obj = n; } return this.getCollection( "$cmd" ).findOne( obj ); } DB.prototype._dbCommand = DB.prototype.runCommand; DB.prototype.addUser = function( username , pass ){ var c = this.getCollection( "system.users" ); var u = c.findOne( { user : username } ) || { user : username }; u.pwd = hex_md5( username + ":mongo:" + pass ); print( tojson( u ) ); c.save( u ); } DB.prototype.removeUser = function( username ){ this.getCollection( "system.users" ).remove( { user : username } ); } DB.prototype.auth = function( username , pass ){ var n = this.runCommand( { getnonce : 1 } ); var a = this.runCommand( { authenticate : 1 , user : username , nonce : n.nonce , key : hex_md5( n.nonce + username + hex_md5( username + ":mongo:" + pass ) ) } ); return a.ok; } /** Create a new collection in the database. Normally, collection creation is automatic. You would use this function if you wish to specify special options on creation. If the collection already exists, no action occurs.

Options:

Example:

db.createCollection("movies", { size: 10 * 1024 * 1024, capped:true } ); * @param {String} name Name of new collection to create * @param {Object} options Object with options for call. Options are listed above. * @return SOMETHING_FIXME */ DB.prototype.createCollection = function(name, opt) { var options = opt || {}; var cmd = { create: name, capped: options.capped, size: options.size, max: options.max }; var res = this._dbCommand(cmd); return res; } /** * Returns the current profiling level of this database * @return SOMETHING_FIXME or null on error */ DB.prototype.getProfilingLevel = function() { var res = this._dbCommand( { profile: -1 } ); return res ? res.was : null; } /** Erase the entire database. (!) * @return Object returned has member ok set to true if operation succeeds, false otherwise. */ DB.prototype.dropDatabase = function() { return this._dbCommand( { dropDatabase: 1 } ); } DB.prototype.shutdownServer = function() { if( "admin" != db ) return "shutdown command only works with the admin database; try 'use admin'"; return this._dbCommand("shutdown"); } /** Clone database on another server to here.

Generally, you should dropDatabase() first as otherwise the cloned information will MERGE into whatever data is already present in this database. (That is however a valid way to use clone if you are trying to do something intentionally, such as union three non-overlapping databases into one.)

This is a low level administrative function will is not typically used. * @param {String} from Where to clone from (dbhostname[:port]). May not be this database (self) as you cannot clone to yourself. * @return Object returned has member ok set to true if operation succeeds, false otherwise. * See also: db.copyDatabase() */ DB.prototype.cloneDatabase = function(from) { assert( isString(from) && from.length ); //this.resetIndexCache(); return this._dbCommand( { clone: from } ); } /** Clone collection on another server to here.

Generally, you should drop() first as otherwise the cloned information will MERGE into whatever data is already present in this collection. (That is however a valid way to use clone if you are trying to do something intentionally, such as union three non-overlapping collections into one.)

This is a low level administrative function is not typically used. * @param {String} from mongod instance from which to clnoe (dbhostname:port). May not be this mongod instance, as clone from self is not allowed. * @param {String} collection name of collection to clone. * @param {Object} query query specifying which elements of collection are to be cloned. * @return Object returned has member ok set to true if operation succeeds, false otherwise. * See also: db.cloneDatabase() */ DB.prototype.cloneCollection = function(from, collection, query) { assert( isString(from) && from.length ); assert( isString(collection) && collection.length ); collection = this._name + "." + collection; query = query || {}; //this.resetIndexCache(); return this._dbCommand( { cloneCollection:collection, from:from, query:query } ); } /** Copy database from one server or name to another server or name. Generally, you should dropDatabase() first as otherwise the copied information will MERGE into whatever data is already present in this database (and you will get duplicate objects in collections potentially.) For security reasons this function only works when executed on the "admin" db. However, if you have access to said db, you can copy any database from one place to another. This method provides a way to "rename" a database by copying it to a new db name and location. Additionally, it effectively provides a repair facility. * @param {String} fromdb database name from which to copy. * @param {String} todb database name to copy to. * @param {String} fromhost hostname of the database (and optionally, ":port") from which to copy the data. default if unspecified is to copy from self. * @return Object returned has member ok set to true if operation succeeds, false otherwise. * See also: db.clone() */ DB.prototype.copyDatabase = function(fromdb, todb, fromhost) { assert( isString(fromdb) && fromdb.length ); assert( isString(todb) && todb.length ); fromhost = fromhost || ""; //this.resetIndexCache(); return this._dbCommand( { copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb } ); } /** Repair database. * @return Object returned has member ok set to true if operation succeeds, false otherwise. */ DB.prototype.repairDatabase = function() { return this._dbCommand( { repairDatabase: 1 } ); } DB.prototype.help = function() { print("DB methods:"); print("\tdb.auth(username, password)"); print("\tdb.getMongo() get the server connection object"); print("\tdb.getSisterDB(name) get the db at the same server as this onew"); print("\tdb.getName()"); print("\tdb.getCollection(cname) same as db['cname'] or db.cname"); print("\tdb.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }"); print("\tdb.commandHelp(name) returns the help for the command"); print("\tdb.addUser(username, password)"); print("\tdb.removeUser(username)"); print("\tdb.createCollection(name, { size : ..., capped : ..., max : ... } )"); print("\tdb.getReplicationInfo()"); print("\tdb.getProfilingLevel()"); print("\tdb.setProfilingLevel(level) 0=off 1=slow 2=all"); print("\tdb.cloneDatabase(fromhost)"); print("\tdb.copyDatabase(fromdb, todb, fromhost)"); print("\tdb.shutdownServer()"); print("\tdb.dropDatabase()"); print("\tdb.repairDatabase()"); print("\tdb.eval(func, args) run code server-side"); print("\tdb.getLastError()"); print("\tdb.getPrevError()"); print("\tdb.resetError()"); print("\tdb.getCollectionNames()"); print("\tdb.group(ns, key[, keyf], cond, reduce, initial)"); } /** *

Set profiling level for your db. Profiling gathers stats on query performance.

* *

Default is off, and resets to off on a database restart -- so if you want it on, * turn it on periodically.

* *

Levels :

*