const char * jsconcatcode = "\n" "\n" "\n" "if ( ( typeof DBCollection ) == \"undefined\" ){\n" "DBCollection = function( mongo , db , shortName , fullName ){\n" "this._mongo = mongo;\n" "this._db = db;\n" "this._shortName = shortName;\n" "this._fullName = fullName;\n" "\n" "this.verify();\n" "}\n" "}\n" "\n" "DBCollection.prototype.verify = function(){\n" "assert( this._fullName , \"no fullName\" );\n" "assert( this._shortName , \"no shortName\" );\n" "assert( this._db , \"no db\" );\n" "\n" "assert.eq( this._fullName , this._db._name + \".\" + this._shortName , \"name mismatch\" );\n" "\n" "assert( this._mongo , \"no mongo in DBCollection\" );\n" "}\n" "\n" "DBCollection.prototype.getName = function(){\n" "return this._shortName;\n" "}\n" "\n" "DBCollection.prototype.help = function(){\n" "print(\"DBCollection help\");\n" "print(\"\\tdb.foo.getDB() get DB object associated with collection\");\n" "print(\"\\tdb.foo.findOne(...)\");\n" "print(\"\\tdb.foo.find(...)\");\n" "print(\"\\tdb.foo.find(...).sort(...)\");\n" "print(\"\\tdb.foo.find(...).limit(n)\");\n" "print(\"\\tdb.foo.find(...).skip(n)\");\n" "print(\"\\tdb.foo.find(...).count()\");\n" "print(\"\\tdb.foo.count()\");\n" "print(\"\\tdb.foo.save(obj)\");\n" "print(\"\\tdb.foo.update(query, object[, upsert_bool])\");\n" "print(\"\\tdb.foo.ensureIndex(keypattern)\");\n" "print(\"\\tdb.foo.dropIndexes()\");\n" "print(\"\\tdb.foo.dropIndex(name)\");\n" "print(\"\\tdb.foo.getIndexes()\");\n" "print(\"\\tdb.foo.drop() drop the collection\");\n" "print(\"\\tdb.foo.validate() - SLOW\");\n" "print(\"\\tdb.foo.stats() - stats about the collection - SLOW\");\n" "print(\"\\tdb.foo.dataSize() - size in bytes of all the data - SLOW\");\n" "print(\"\\tdb.foo.totalIndexSize() - size in bytes of all the indexes - SLOW\");\n" "}\n" "\n" "DBCollection.prototype.getFullName = function(){\n" "return this._fullName;\n" "}\n" "DBCollection.prototype.getDB = function(){\n" "return this._db;\n" "}\n" "\n" "DBCollection.prototype._dbCommand = function( cmd ){\n" "return this._db._dbCommand( cmd );\n" "}\n" "\n" "DBCollection.prototype._massageObject = function( q ){\n" "if ( ! q )\n" "return {};\n" "\n" "var type = typeof q;\n" "\n" "if ( type == \"function\" )\n" "return { $where : q };\n" "\n" "if ( q.isObjectId )\n" "return { _id : q };\n" "\n" "if ( type == \"object\" )\n" "return q;\n" "\n" "if ( type == \"string\" ){\n" "if ( q.length == 24 )\n" "return { _id : q };\n" "\n" "return { $where : q };\n" "}\n" "\n" "throw \"don't know how to massage : \" + type;\n" "\n" "}\n" "\n" "\n" "DBCollection.prototype._validateObject = function( o ){\n" "if ( o._ensureSpecial && o._checkModify )\n" "throw \"can't save a DBQuery object\";\n" "}\n" "\n" "DBCollection.prototype._validateForStorage = function( o ){\n" "this._validateObject( o );\n" "for ( var k in o ){\n" "if ( k.indexOf( \".\" ) >= 0 )\n" "throw \"can't have . in field names [\" + k + \"]\" ;\n" "}\n" "}\n" "\n" "\n" "DBCollection.prototype.find = function( query , fields , limit , skip ){\n" "return new DBQuery( this._mongo , this._db , this ,\n" "this._fullName , this._massageObject( query ) , fields , limit , skip );\n" "}\n" "\n" "DBCollection.prototype.findOne = function( query , fields ){\n" "var cursor = this._mongo.find( this._fullName , this._massageObject( query ) || {} , fields , -1 , 0 );\n" "if ( ! cursor.hasNext() )\n" "return null;\n" "var ret = cursor.next();\n" "if ( cursor.hasNext() ) throw \"findOne has more than 1 result!\";\n" "if ( ret.$err )\n" "throw \"error \" + tojson( ret );\n" "return ret;\n" "}\n" "\n" "DBCollection.prototype.insert = function( obj ){\n" "if ( ! obj )\n" "throw \"no object!\";\n" "this._validateForStorage( obj );\n" "return this._mongo.insert( this._fullName , obj );\n" "}\n" "\n" "DBCollection.prototype.remove = function( t ){\n" "this._mongo.remove( this._fullName , this._massageObject( t ) );\n" "}\n" "\n" "DBCollection.prototype.update = function( query , obj , upsert ){\n" "assert( query , \"need a query\" );\n" "assert( obj , \"need an object\" );\n" "this._validateObject( obj );\n" "return this._mongo.update( this._fullName , query , obj , upsert ? true : false );\n" "}\n" "\n" "DBCollection.prototype.save = function( obj ){\n" "if ( typeof( obj._id ) == \"undefined\" ){\n" "obj._id = new ObjectId();\n" "return this.insert( obj );\n" "}\n" "else {\n" "return this.update( { _id : obj._id } , obj , true );\n" "}\n" "}\n" "\n" "DBCollection.prototype._genIndexName = function( keys ){\n" "var name = \"\";\n" "for ( var k in keys ){\n" "if ( name.length > 0 )\n" "name += \"_\";\n" "name += k + \"_\";\n" "\n" "var v = keys[k];\n" "if ( typeof v == \"number\" )\n" "name += v;\n" "}\n" "return name;\n" "}\n" "\n" "DBCollection.prototype._indexSpec = function( keys, options ) {\n" "var name;\n" "var unique = false;\n" "if ( !isObject( options ) ) {\n" "options = [ options ];\n" "}\n" "for( var i = 0; i < options.length; ++i ) {\n" "var o = options[ i ];\n" "if ( isString( o ) ) {\n" "name = o;\n" "} else if ( typeof( o ) == \"boolean\" ) {\n" "unique = o;\n" "}\n" "}\n" "name = name || this._genIndexName( keys );\n" "var ret = { ns : this._fullName , key : keys , name : name };\n" "if ( unique == true ) {\n" "ret.unique = true;\n" "}\n" "return ret;\n" "}\n" "\n" "DBCollection.prototype.createIndex = function( keys , options ){\n" "var o = this._indexSpec( keys, options );\n" "this._db.getCollection( \"system.indexes\" ).insert( o );\n" "}\n" "\n" "DBCollection.prototype.ensureIndex = function( keys , options ){\n" "var name = this._indexSpec( keys, options ).name;\n" "this._indexCache = this._indexCache || {};\n" "if ( this._indexCache[ name ] ){\n" "return false;\n" "}\n" "\n" "this.createIndex( keys , options );\n" "this._indexCache[name] = true;\n" "return true;\n" "}\n" "\n" "DBCollection.prototype.resetIndexCache = function(){\n" "this._indexCache = {};\n" "}\n" "\n" "DBCollection.prototype.reIndex = function(){\n" "var specs = this.getIndexSpecs();\n" "this.dropIndexes();\n" "for ( var i = 0; i < specs.length; ++i ){\n" "this.ensureIndex( specs[i].key, [ specs[i].unique, specs[i].name ] );\n" "}\n" "}\n" "\n" "DBCollection.prototype.dropIndexes = function(){\n" "this.resetIndexCache();\n" "\n" "var res = this._db.runCommand( { deleteIndexes: this.getName(), index: \"*\" } );\n" "assert( res , \"no result from dropIndex result\" );\n" "if ( res.ok )\n" "return res;\n" "\n" "if ( res.errmsg.match( /not found/ ) )\n" "return res;\n" "\n" "throw \"error dropping indexes : \" + tojson( res );\n" "}\n" "\n" "\n" "DBCollection.prototype.drop = function(){\n" "this.resetIndexCache();\n" "return this._db.runCommand( { drop: this.getName() } );\n" "}\n" "\n" "DBCollection.prototype.validate = function() {\n" "var res = this._db.runCommand( { validate: this.getName() } );\n" "\n" "res.valid = false;\n" "\n" "if ( res.result ){\n" "var str = \"-\" + tojson( res.result );\n" "res.valid = ! ( str.match( /exception/ ) || str.match( /corrupt/ ) );\n" "\n" "var p = /lastExtentSize:(\\d+)/;\n" "var r = p.exec( str );\n" "if ( r ){\n" "res.lastExtentSize = Number( r[1] );\n" "}\n" "}\n" "\n" "return res;\n" "}\n" "\n" "DBCollection.prototype.getIndexes = function(){\n" "return this.getDB().getCollection( \"system.indexes\" ).find( { ns : this.getFullName() } ).toArray();\n" "}\n" "\n" "DBCollection.prototype.getIndices = DBCollection.prototype.getIndexes;\n" "DBCollection.prototype.getIndexSpecs = DBCollection.prototype.getIndexes;\n" "\n" "DBCollection.prototype.getIndexKeys = function(){\n" "return this.getIndexes().map(\n" "function(i){\n" "return i.key;\n" "}\n" ");\n" "}\n" "\n" "\n" "DBCollection.prototype.count = function( x ){\n" "return this.find( x ).count();\n" "}\n" "\n" "/**\n" "* Drop free lists. Normally not used.\n" "* Note this only does the collection itself, not the namespaces of its indexes (see cleanAll).\n" "*/\n" "DBCollection.prototype.clean = function() {\n" "return this._dbCommand( { clean: this.getName() } );\n" "}\n" "\n" "\n" "\n" "/**\n" "*

Drop a specified index.

\n" "*\n" "*

\n" "* Name is the name of the index in the system.indexes name field. (Run db.system.indexes.find() to\n" "* see example data.)\n" "*

\n" "*\n" "*

Note : alpha: space is not reclaimed

\n" "* @param {String} name of index to delete.\n" "* @return A result object. result.ok will be true if successful.\n" "*/\n" "DBCollection.prototype.dropIndex = function(index) {\n" "assert(index , \"need to specify index to dropIndex\" );\n" "\n" "if ( ! isString( index ) && isObject( index ) )\n" "index = this._genIndexName( index );\n" "\n" "var res = this._dbCommand( { deleteIndexes: this.getName(), index: index } );\n" "this.resetIndexCache();\n" "return res;\n" "}\n" "\n" "DBCollection.prototype.copyTo = function( newName ){\n" "return this.getDB().eval(\n" "function( collName , newName ){\n" "var from = db[collName];\n" "var to = db[newName];\n" "to.ensureIndex( { _id : 1 } );\n" "var count = 0;\n" "\n" "var cursor = from.find();\n" "while ( cursor.hasNext() ){\n" "var o = cursor.next();\n" "count++;\n" "to.save( o );\n" "}\n" "\n" "return count;\n" "} , this.getName() , newName\n" ");\n" "}\n" "\n" "DBCollection.prototype.getCollection = function( subName ){\n" "return this._db.getCollection( this._shortName + \".\" + subName );\n" "}\n" "\n" "DBCollection.prototype.stats = function(){\n" "var res = this.validate().result;\n" "var p = /\\b(\\w+)\\??: *(\\d+)\\b/g;\n" "var m;\n" "\n" "var o = {};\n" "while ( m = p.exec( res ) ){\n" "o[ m[1] ] = m[2];\n" "}\n" "return o;\n" "}\n" "\n" "DBCollection.prototype.dataSize = function(){\n" "return parseInt( this.stats().datasize );\n" "}\n" "\n" "DBCollection.prototype.totalIndexSize = function(){\n" "var total = 0;\n" "var mydb = this._db;\n" "var shortName = this._shortName;\n" "this.getIndexes().forEach(\n" "function( spec ){\n" "var coll = mydb.getCollection( shortName + \".$\" + spec.name );\n" "var mysize = coll.dataSize();\n" "\n" "total += coll.dataSize();\n" "}\n" ");\n" "return total;\n" "}\n" "\n" "DBCollection.prototype.convertToCapped = function( bytes ){\n" "if ( ! bytes )\n" "throw \"have to specify # of bytes\";\n" "return this._dbCommand( { convertToCapped : this._shortName , size : bytes } )\n" "}\n" "\n" "DBCollection.prototype.exists = function(){\n" "return this._db.system.namespaces.findOne( { name : this._fullName } );\n" "}\n" "\n" "DBCollection.prototype.isCapped = function(){\n" "var e = this.exists();\n" "return ( e && e.options && e.options.capped ) ? true : false;\n" "}\n" "\n" "DBCollection.prototype.group = function( params ){\n" "params.ns = this._shortName;\n" "return this._db.group( params );\n" "}\n" "\n" "DBCollection.prototype.toString = function(){\n" "return this.getFullName();\n" "}\n" "\n" "DBCollection.prototype.shellPrint = DBCollection.prototype.toString;\n" "\n" "\n" "\n" "\n" "if ( typeof DB == \"undefined\" ){\n" "DB = function( mongo , name ){\n" "this._mongo = mongo;\n" "this._name = name;\n" "}\n" "}\n" "\n" "DB.prototype.getMongo = function(){\n" "assert( this._mongo , \"why no mongo!\" );\n" "return this._mongo;\n" "}\n" "\n" "DB.prototype.getSisterDB = function( name ){\n" "return this.getMongo().getDB( name );\n" "}\n" "\n" "DB.prototype.getName = function(){\n" "return this._name;\n" "}\n" "\n" "DB.prototype.getCollection = function( name ){\n" "return new DBCollection( this._mongo , this , name , this._name + \".\" + name );\n" "}\n" "\n" "DB.prototype.commandHelp = function( name ){\n" "var c = {};\n" "c[name] = 1;\n" "c.help = true;\n" "return this.runCommand( c ).help;\n" "}\n" "\n" "DB.prototype.runCommand = function( obj ){\n" "if ( typeof( obj ) == \"string\" ){\n" "var n = {};\n" "n[obj] = 1;\n" "obj = n;\n" "}\n" "return this.getCollection( \"$cmd\" ).findOne( obj );\n" "}\n" "\n" "DB.prototype._dbCommand = DB.prototype.runCommand;\n" "\n" "DB.prototype.addUser = function( username , pass ){\n" "var c = this.getCollection( \"system.users\" );\n" "\n" "var u = c.findOne( { user : username } ) || { user : username };\n" "u.pwd = hex_md5( username + \":mongo:\" + pass );\n" "print( tojson( u ) );\n" "\n" "c.save( u );\n" "}\n" "\n" "DB.prototype.removeUser = function( username ){\n" "this.getCollection( \"system.users\" ).remove( { user : username } );\n" "}\n" "\n" "DB.prototype.auth = function( username , pass ){\n" "var n = this.runCommand( { getnonce : 1 } );\n" "\n" "var a = this.runCommand(\n" "{\n" "authenticate : 1 ,\n" "user : username ,\n" "nonce : n.nonce ,\n" "key : hex_md5( n.nonce + username + hex_md5( username + \":mongo:\" + pass ) )\n" "}\n" ");\n" "\n" "return a.ok;\n" "}\n" "\n" "/**\n" "Create a new collection in the database. Normally, collection creation is automatic. You would\n" "use this function if you wish to specify special options on creation.\n" "\n" "If the collection already exists, no action occurs.\n" "\n" "

Options:

\n" "\n" "\n" "

Example:

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

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

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

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

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

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

\n" "*\n" "*

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

\n" "*\n" "*

Levels :

\n" "*