From 87a7b96365801d1ee4a55b65d5177f202e7fc903 Mon Sep 17 00:00:00 2001 From: Adam Midvidy Date: Tue, 19 May 2015 11:47:32 -0400 Subject: [PATCH] SERVER-18557 remove usage of $cmd.findOne from shell and jstests --- jstests/core/count5.js | 4 ++-- jstests/gle/core/error1.js | 28 ++++++++++++++-------------- src/mongo/shell/db.js | 18 ++++++++++-------- src/mongo/shell/explain_query.js | 18 ++++-------------- 4 files changed, 30 insertions(+), 38 deletions(-) diff --git a/jstests/core/count5.js b/jstests/core/count5.js index b6bbc543352..3d7cf04a27c 100644 --- a/jstests/core/count5.js +++ b/jstests/core/count5.js @@ -26,5 +26,5 @@ assert.eq( 2 , t.find( q ).skip(48).limit(5).size() , "I" ); assert.eq( 20 , t.find().limit(20).size() , "J" ); assert.eq( 0 , t.find().skip(120).size() , "K" ); -assert.eq( 1 , db.$cmd.findOne( { count: "count5" } )["ok"] , "L" ); -assert.eq( 1 , db.$cmd.findOne( { count: "count5", skip: 120 } )["ok"] , "M" ); +assert.eq( 1 , db.runCommand( { count: "count5" } )["ok"] , "L" ); +assert.eq( 1 , db.runCommand( { count: "count5", skip: 120 } )["ok"] , "M" ); diff --git a/jstests/gle/core/error1.js b/jstests/gle/core/error1.js index 4043bff4783..b29986976c4 100644 --- a/jstests/gle/core/error1.js +++ b/jstests/gle/core/error1.js @@ -1,9 +1,9 @@ db.jstests_error1.drop(); // test 1 -db.$cmd.findOne({reseterror:1}); -assert( db.$cmd.findOne({getlasterror:1}).err == null, "A" ); -assert( db.$cmd.findOne({getpreverror:1}).err == null, "B" ); +db.runCommand({reseterror:1}); +assert( db.runCommand({getlasterror:1}).err == null, "A" ); +assert( db.runCommand({getpreverror:1}).err == null, "B" ); db.resetError(); assert( db.getLastError() == null, "C" ); @@ -11,9 +11,9 @@ assert( db.getPrevError().err == null , "preverror 1" ); // test 2 -db.$cmd.findOne({forceerror:1}); -assert( db.$cmd.findOne({getlasterror:1}).err != null, "D" ); -assert( db.$cmd.findOne({getpreverror:1}).err != null, "E" ); +db.runCommand({forceerror:1}); +assert( db.runCommand({getlasterror:1}).err != null, "D" ); +assert( db.runCommand({getpreverror:1}).err != null, "E" ); assert( db.getLastError() != null, "F" ); @@ -21,14 +21,14 @@ assert( db.getPrevError().err != null , "preverror 2" ); assert( db.getPrevError().nPrev == 1, "G" ); db.jstests_error1.findOne(); -assert( db.$cmd.findOne({getlasterror:1}).err == null, "H" ); -assert( db.$cmd.findOne({getpreverror:1}).err != null, "I" ); -assert( db.$cmd.findOne({getpreverror:1}).nPrev == 2, "J" ); +assert( db.runCommand({getlasterror:1}).err == null, "H" ); +assert( db.runCommand({getpreverror:1}).err != null, "I" ); +assert( db.runCommand({getpreverror:1}).nPrev == 2, "J" ); db.jstests_error1.findOne(); -assert( db.$cmd.findOne({getlasterror:1}).err == null, "K" ); -assert( db.$cmd.findOne({getpreverror:1}).err != null, "L" ); -assert( db.$cmd.findOne({getpreverror:1}).nPrev == 3, "M" ); +assert( db.runCommand({getlasterror:1}).err == null, "K" ); +assert( db.runCommand({getpreverror:1}).err != null, "L" ); +assert( db.runCommand({getpreverror:1}).nPrev == 3, "M" ); db.resetError(); db.forceError(); @@ -37,5 +37,5 @@ assert( db.getLastError() == null , "getLastError 5" ); assert( db.getPrevError().err != null , "preverror 3" ); // test 3 -db.$cmd.findOne({reseterror:1}); -assert( db.$cmd.findOne({getpreverror:1}).err == null, "N" ); +db.runCommand({reseterror:1}); +assert( db.runCommand({getpreverror:1}).err == null, "N" ); diff --git a/src/mongo/shell/db.js b/src/mongo/shell/db.js index c29884b045d..912f415eebc 100644 --- a/src/mongo/shell/db.js +++ b/src/mongo/shell/db.js @@ -45,11 +45,10 @@ DB.prototype.commandHelp = function( name ){ } // utility to attach readPreference if needed. - DB.prototype._attachReadPreferenceToCommand = function (cmdObj) { + DB.prototype._attachReadPreferenceToCommand = function (cmdObj, readPref) { "use strict"; - var readPref = this.getMongo().getReadPref(); // if the user has not set a readpref, return the original cmdObj - if (readPref === null) { + if ((readPref === null) || typeof(readPref) !== "object") { return cmdObj; } @@ -86,21 +85,24 @@ DB.prototype.commandHelp = function( name ){ }; // Like runCommand but applies readPreference if one has been set - // on the connection. Also sets options automatically. + // 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(obj); + var cmdObjWithReadPref = + this._attachReadPreferenceToCommand(mergedObj, + this.getMongo().getReadPref()); var options = 0; - // automatically set slaveOk if readPreference is anything but primary + // We automatically set slaveOk if readPreference is anything but primary. if (this.getMongo().getReadPrefMode() !== "primary") { options |= 4; } - // allow options to be overridden - // extra is not used since mergedObj is an object + // The 'extra' parameter is not used as we have already created a merged command object. return this.runCommand(cmdObjWithReadPref, null, options); }; diff --git a/src/mongo/shell/explain_query.js b/src/mongo/shell/explain_query.js index 1c7d25ee5f1..1d7b1008afd 100644 --- a/src/mongo/shell/explain_query.js +++ b/src/mongo/shell/explain_query.js @@ -160,25 +160,15 @@ var DBExplainQuery = (function() { var explainCmd = {explain: innerCmd}; explainCmd["verbosity"] = this._verbosity; - // We explicitly run a find against the $cmd collection instead of using the - // runCommand helper so that we can set a read preference on the command. - var cmdColl = this._query._db.getCollection("$cmd"); - var cmdQuery = cmdColl.find(explainCmd, - {}, // projection - -1, // limit - 0, // skip - 0, // batchSize - this._query._options); + var explainDb = this._query._db; - // Handle read preference. if ("$readPreference" in this._query._query) { - // A read preference was set on the query. Pull the read pref up so that it is - // set on the explain command. var prefObj = this._query._query.$readPreference; - cmdQuery.readPref(prefObj.mode, prefObj.tags); + explainCmd = explainDb._attachReadPreferenceToCommand(explainCmd, prefObj); } - var explainResult = cmdQuery.next(); + var explainResult = explainDb.runCommand(explainCmd, null, this._query._options); + if (!explainResult.ok && explainResult.code === CMD_NOT_FOUND_CODE) { // One of the shards doesn't have the explain command available. Retry using // the legacy $explain format, which should be supported by all shards.