// db.js var DB; (function() { if (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.getSiblingDB = function( name ){ return this.getMongo().getDB( name ); } DB.prototype.getSisterDB = DB.prototype.getSiblingDB; DB.prototype.getName = function(){ return this._name; } DB.prototype.stats = function(scale){ return this.runCommand( { dbstats : 1 , scale : scale } ); } 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; var res = this.runCommand( c ); if ( ! res.ok ) throw Error(res.errmsg); return res.help; } // utility to attach readPreference if needed. DB.prototype._attachReadPreferenceToCommand = function (cmdObj, readPref) { "use strict"; // if the user has not set a readpref, return the original cmdObj if ((readPref === null) || typeof(readPref) !== "object") { return cmdObj; } // if user specifies $readPreference manually, then don't change it if (cmdObj.hasOwnProperty("$readPreference")) { return cmdObj; } // copy object so we don't mutate the original var clonedCmdObj = Object.extend({}, cmdObj); // The server selection spec mandates that the key is '$query', but // the shell has historically used 'query'. The server accepts both, // so we maintain the existing behavior var cmdObjWithReadPref = { query: clonedCmdObj, $readPreference: readPref }; return cmdObjWithReadPref; }; // if someone passes i.e. runCommand("foo", {bar: "baz"} // we merge it in to runCommand({foo: 1, bar: "baz"} // this helper abstracts that logic. DB.prototype._mergeCommandOptions = function(commandName, extraKeys) { "use strict"; var mergedCmdObj = {}; mergedCmdObj[commandName] = 1; if (typeof(extraKeys) === "object") { // this will traverse the prototype chain of extra, but keeping // to maintain legacy behavior for (var key in extraKeys) { mergedCmdObj[key] = extraKeys[key]; } } return mergedCmdObj; }; // Like runCommand but applies readPreference if one has been set // on the connection. Also sets slaveOk if a (non-primary) readPref has been set. DB.prototype.runReadCommand = function (obj, extra) { "use strict"; // Support users who call this function with a string commandName, e.g. // db.runReadCommand("commandName", {arg1: "value", arg2: "value"}). var mergedObj = (typeof(obj) === "string") ? this._mergeCommandOptions(obj, extra) : obj; var cmdObjWithReadPref = this._attachReadPreferenceToCommand(mergedObj, this.getMongo().getReadPref()); var options = 0; // We automatically set slaveOk if readPreference is anything but primary. if (this.getMongo().getReadPrefMode() !== "primary") { options |= 4; } // The 'extra' parameter is not used as we have already created a merged command object. return this.runCommand(cmdObjWithReadPref, null, options); }; DB.prototype.runCommand = function( obj, extra, queryOptions ){ var mergedObj = (typeof(obj) === "string") ? this._mergeCommandOptions(obj, extra) : obj; // if options were passed (i.e. because they were overridden on a collection), use them. // Otherwise use getQueryOptions. var options = (typeof(queryOptions) !== "undefined") ? queryOptions : this.getQueryOptions(); var res; try { res = this.getMongo().runCommand(this._name, mergedObj, options); } catch (ex) { // When runCommand flowed through query, a connection error resulted in the message // "error doing query: failed". Even though this message is arguably incorrect // for a command failing due to a connection failure, we preserve it for backwards // compatibility. See SERVER-18334 for details. if (ex.message.indexOf("network error") >= 0) { throw new Error("error doing query: failed"); } throw ex; } return res; }; DB.prototype._dbCommand = DB.prototype.runCommand; DB.prototype._dbReadCommand = DB.prototype.runReadCommand; DB.prototype.adminCommand = function( obj, extra ){ if ( this._name == "admin" ) return this.runCommand( obj, extra ); return this.getSiblingDB( "admin" ).runCommand( obj, extra ); } DB.prototype._adminCommand = DB.prototype.adminCommand; // alias old name /** 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 || {};
// We have special handling for the 'flags' field, and provide sugar for specific flags. If the
// user specifies any flags we send the field in the command. Otherwise, we leave it blank and
// use the server's defaults.
var sendFlags = false;
var flags = 0;
if (options.usePowerOf2Sizes != undefined) {
print("WARNING: The 'usePowerOf2Sizes' flag is ignored in 3.0 and higher as all MMAPv1 "
+ "collections use fixed allocation sizes unless the 'noPadding' flag is specified");
sendFlags = true;
if (options.usePowerOf2Sizes) {
flags |= 1; // Flag_UsePowerOf2Sizes
}
delete options.usePowerOf2Sizes;
}
if (options.noPadding != undefined) {
sendFlags = true;
if (options.noPadding) {
flags |= 2; // Flag_NoPadding
}
delete options.noPadding;
}
// New flags must be added above here.
if (sendFlags) {
if (options.flags != undefined)
throw Error("Can't set 'flags' with either 'usePowerOf2Sizes' or 'noPadding'");
options.flags = flags;
}
var cmd = { create: name };
Object.extend(cmd, options);
return this._dbCommand(cmd);
}
/**
* @deprecated use getProfilingStatus
* 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;
}
/**
* @return the current profiling status
* example { was : 0, slowms : 100 }
* @return SOMETHING_FIXME or null on error
*/
DB.prototype.getProfilingStatus = function() {
var res = this._dbCommand( { profile: -1 } );
if ( ! res.ok )
throw Error( "profile command failed: " + tojson( res ) );
delete res.ok
return res;
}
/**
Erase the entire database. (!)
* @return Object returned has member ok set to true if operation succeeds, false otherwise.
*/
DB.prototype.dropDatabase = function() {
if ( arguments.length )
throw Error("dropDatabase doesn't take arguments");
return this._dbCommand( { dropDatabase: 1 } );
}
/**
* Shuts down the database. Must be run while using the admin database.
* @param opts Options for shutdown. Possible options are:
* - force: (boolean) if the server should shut down, even if there is no
* up-to-date slave
* - timeoutSecs: (number) the server will continue checking over timeoutSecs
* if any other servers have caught up enough for it to shut down.
*/
DB.prototype.shutdownServer = function(opts) {
if( "admin" != this._name ){
return "shutdown command only works with the admin database; try 'use admin'";
}
var cmd = {'shutdown' : 1};
opts = opts || {};
for (var o in opts) {
cmd[o] = opts[o];
}
try {
var res = this.runCommand(cmd);
if (!res.ok) {
throw Error('shutdownServer failed: ' + tojson(res));
}
throw Error('shutdownServer failed: server is still up.');
}
catch (e) {
// we expect the command to not return a response, as the server will shut down immediately.
if (e.message === "error doing query: failed") {
print('server should be down...');
return;
}
throw e;
}
}
/**
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 ); 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 || {};
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, username, password, mechanism) {
assert( isString(fromdb) && fromdb.length );
assert( isString(todb) && todb.length );
fromhost = fromhost || "";
if (!mechanism) {
mechanism = this._getDefaultAuthenticationMechanism();
}
assert(mechanism == "SCRAM-SHA-1" || mechanism == "MONGODB-CR");
// Check for no auth or copying from localhost
if (!username || !password || fromhost == "") {
return this._adminCommand( { copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb } );
}
// Use the copyDatabase native helper for SCRAM-SHA-1
if (mechanism == "SCRAM-SHA-1") {
return this.getMongo().copyDatabaseWithSCRAM(fromdb, todb, fromhost, username, password);
}
// Fall back to MONGODB-CR
var n = this._adminCommand({ copydbgetnonce : 1, fromhost:fromhost});
return this._adminCommand({ copydb:1, fromhost:fromhost, fromdb:fromdb, todb:todb,
username:username, nonce:n.nonce,
key:this.__pwHash(n.nonce, username, password) });
}
/**
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.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]");
print("\tdb.auth(username, password)");
print("\tdb.cloneDatabase(fromhost)");
print("\tdb.commandHelp(name) returns the help for the command");
print("\tdb.copyDatabase(fromdb, todb, fromhost)");
print("\tdb.createCollection(name, { size : ..., capped : ..., max : ... } )");
print("\tdb.createUser(userDocument)");
print("\tdb.currentOp() displays currently executing operations in the db");
print("\tdb.dropDatabase()");
print("\tdb.eval() - deprecated");
print("\tdb.fsyncLock() flush data to disk and lock server for backups");
print("\tdb.fsyncUnlock() unlocks server following a db.fsyncLock()");
print("\tdb.getCollection(cname) same as db['cname'] or db.cname");
print("\tdb.getCollectionInfos([filter]) - returns a list that contains the names and options"
+ " of the db's collections");
print("\tdb.getCollectionNames()");
print("\tdb.getLastError() - just returns the err msg string");
print("\tdb.getLastErrorObj() - return full status object");
print("\tdb.getLogComponents()");
print("\tdb.getMongo() get the server connection object");
print("\tdb.getMongo().setSlaveOk() allow queries on a replication slave server");
print("\tdb.getName()");
print("\tdb.getPrevError()");
print("\tdb.getProfilingLevel() - deprecated");
print("\tdb.getProfilingStatus() - returns if profiling is on and slow threshold");
print("\tdb.getReplicationInfo()");
print("\tdb.getSiblingDB(name) get the db at the same server as this one");
print("\tdb.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set");
print("\tdb.hostInfo() get details about the server's host");
print("\tdb.isMaster() check replica primary status");
print("\tdb.killOp(opid) kills the current operation in the db");
print("\tdb.listCommands() lists all the db commands");
print("\tdb.loadServerScripts() loads all the scripts in db.system.js");
print("\tdb.logout()");
print("\tdb.printCollectionStats()");
print("\tdb.printReplicationInfo()");
print("\tdb.printShardingStatus()");
print("\tdb.printSlaveReplicationInfo()");
print("\tdb.dropUser(username)");
print("\tdb.repairDatabase()");
print("\tdb.resetError()");
print("\tdb.runCommand(cmdObj) run a database command. if cmdObj is a string, turns it into { cmdObj : 1 }");
print("\tdb.serverStatus()");
print("\tdb.setLogLevel(level, 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 : Evaluate a js expression at the database server. Useful if you need to touch a lot of data lightly; in such a scenario
* the network transfer of the data could be a bottleneck. A good example
* is "select count(*)" -- can be done server side via this mechanism.
*
* If the eval fails, an exception is thrown of the form:
* Example:
* Similar to SQL group by. For example:
* corresponds to the following in 10gen:
*
* An array of grouped items is returned. The array must fit in RAM, thus this function is not
* suitable when the return set is extremely large.
*
* To order the grouped data, simply sort it client side upon return.
*
Defaults
cond may be null if you want to run against all rows in the collection
keyf is a function which takes an object and returns the desired key. set either key or keyf (not both).
*
This command is for the database/cloud administer and not applicable to most databases.
It is only used with the local database. One might invoke from the JS shell:
* @return Object timeSpan: time span of the oplog from start to end if slave is more out
* of date than that, it can't recover without a complete resync
*/
DB.prototype.getReplicationInfo = function() {
var localdb = this.getSiblingDB("local");
var result = { };
var oplog;
var localCollections = localdb.getCollectionNames();
if (localCollections.indexOf('oplog.rs') >= 0) {
oplog = 'oplog.rs';
}
else if (localCollections.indexOf('oplog.$main') >= 0) {
oplog = 'oplog.$main';
}
else {
result.errmsg = "neither master/slave nor replica set replication detected";
return result;
}
var ol = localdb.getCollection(oplog);
var ol_stats = ol.stats();
if( ol_stats && ol_stats.maxSize ) {
result.logSizeMB = ol_stats.maxSize / ( 1024 * 1024 );
} else {
result.errmsg = "Could not get stats for local."+oplog+" collection. " +
"collstats returned: " + tojson(ol_stats);
return result;
}
result.usedMB = ol_stats.size / ( 1024 * 1024 );
result.usedMB = Math.ceil( result.usedMB * 100 ) / 100;
var firstc = ol.find().sort({$natural:1}).limit(1);
var lastc = ol.find().sort({$natural:-1}).limit(1);
if( !firstc.hasNext() || !lastc.hasNext() ) {
result.errmsg = "objects not found in local.oplog.$main -- is this a new and empty db instance?";
result.oplogMainRowCount = ol.count();
return result;
}
var first = firstc.next();
var last = lastc.next();
var tfirst = first.ts;
var tlast = last.ts;
if( tfirst && tlast ) {
tfirst = DB.tsToSeconds( tfirst );
tlast = DB.tsToSeconds( tlast );
result.timeDiff = tlast - tfirst;
result.timeDiffHours = Math.round(result.timeDiff / 36)/100;
result.tFirst = (new Date(tfirst*1000)).toString();
result.tLast = (new Date(tlast*1000)).toString();
result.now = Date();
}
else {
result.errmsg = "ts element not found in oplog objects";
}
return result;
};
DB.prototype.printReplicationInfo = function() {
var result = this.getReplicationInfo();
if( result.errmsg ) {
if (!this.isMaster().ismaster) {
print("this is a slave, printing slave replication info.");
this.printSlaveReplicationInfo();
return;
}
print(tojson(result));
return;
}
print("configured oplog size: " + result.logSizeMB + "MB");
print("log length start to end: " + result.timeDiff + "secs (" + result.timeDiffHours + "hrs)");
print("oplog first event time: " + result.tFirst);
print("oplog last event time: " + result.tLast);
print("now: " + result.now);
}
DB.prototype.printSlaveReplicationInfo = function() {
var startOptimeDate = null;
var primary = null;
function getReplLag(st) {
assert( startOptimeDate , "how could this be null (getReplLag startOptimeDate)" );
print("\tsyncedTo: " + st.toString() );
var ago = (startOptimeDate-st)/1000;
var hrs = Math.round(ago/36)/100;
var suffix = "";
if (primary) {
suffix = "primary ";
}
else {
suffix = "freshest member (no primary available at the moment)";
}
print("\t" + Math.round(ago) + " secs (" + hrs + " hrs) behind the " + suffix);
};
function getMaster(members) {
for (i in members) {
var row = members[i];
if (row.state === 1) {
return row;
}
}
return null;
};
function g(x) {
assert( x , "how could this be null (printSlaveReplicationInfo gx)" )
print("source: " + x.host);
if ( x.syncedTo ){
var st = new Date( DB.tsToSeconds( x.syncedTo ) * 1000 );
getReplLag(st);
}
else {
print( "\tdoing initial sync" );
}
};
function r(x) {
assert( x , "how could this be null (printSlaveReplicationInfo rx)" );
if ( x.state == 1 || x.state == 7 ) { // ignore primaries (1) and arbiters (7)
return;
}
print("source: " + x.name);
if ( x.optime ) {
getReplLag(x.optimeDate);
}
else {
print( "\tno replication info, yet. State: " + x.stateStr );
}
};
var L = this.getSiblingDB("local");
if (L.system.replset.count() != 0) {
var status = this.adminCommand({'replSetGetStatus' : 1});
primary = getMaster(status.members);
if (primary) {
startOptimeDate = primary.optimeDate;
}
// no primary, find the most recent op among all members
else {
startOptimeDate = new Date(0, 0);
for (i in status.members) {
if (status.members[i].optimeDate > startOptimeDate) {
startOptimeDate = status.members[i].optimeDate;
}
}
}
for (i in status.members) {
r(status.members[i]);
}
}
else if( L.sources.count() != 0 ) {
startOptimeDate = new Date();
L.sources.find().forEach(g);
}
else {
print("local.sources is empty; is this db a --slave?");
return;
}
}
DB.prototype.serverBuildInfo = function(){
return this._adminCommand( "buildinfo" );
}
// Used to trim entries from the metrics.commands that have never been executed
getActiveCommands = function(tree) {
var result = { };
for (var i in tree) {
if (!tree.hasOwnProperty(i))
continue;
if (tree[i].hasOwnProperty("total")) {
if (tree[i].total > 0) {
result[i] = tree[i];
}
continue;
}
if (i == "
*
{ dbEvalException: { retval: functionReturnValue, ok: num [, errno: num] [, errmsg: str] } }
*
* print( "mycount: " + db.eval( function(){db.mycoll.find({},{_id:ObjId()}).length();} );
*
* @param {Function} jsfunction Javascript function to run on server. Note this it not a closure, but rather just "code".
* @return result of your function, or null if error
*
*/
DB.prototype.eval = function(jsfunction) {
print("WARNING: db.eval is deprecated");
var cmd = { $eval : jsfunction };
if ( arguments.length > 1 ) {
cmd.args = argumentsToArray( arguments ).slice(1);
}
var res = this._dbCommand( cmd );
if (!res.ok)
throw Error( tojson( res ) );
return res.retval;
}
DB.prototype.dbEval = DB.prototype.eval;
/**
*
* select a,b,sum(c) csum from coll where active=1 group by a,b
*
*
db.group(
{
ns: "coll",
key: { a:true, b:true },
// keyf: ...,
cond: { active:1 },
reduce: function(obj,prev) { prev.csum += obj.c; },
initial: { csum: 0 }
});
*
*
*
use local
db.getReplicationInfo();
It is assumed that this database is a replication master -- the information returned is
about the operation log stored at local.oplog.$main on the replication master. (It also
works on a machine in a replica pair: for replica pairs, both machines are "masters" from
an internal database perspective.