diff options
author | David Storch <david.storch@10gen.com> | 2015-03-19 15:27:54 -0400 |
---|---|---|
committer | David Storch <david.storch@10gen.com> | 2015-03-23 17:45:59 -0400 |
commit | 2729de652a23d33aefc6c5ea89e6593c83b25c7f (patch) | |
tree | 007d9c94479d2bd3cb32c8d891c31888bdc90ec8 /src/mongo/shell | |
parent | a33b1a8688acdd296784e04a8bcb3933a2932247 (diff) | |
download | mongo-2729de652a23d33aefc6c5ea89e6593c83b25c7f.tar.gz |
SERVER-17286 add --readMode flag to shell
Passing '--readMode commands' to the shell will now cause it to run find using the find command,
combined with wire protocol OP_GET_MORE messages. The default readMode remains OP_QUERY style find.
Diffstat (limited to 'src/mongo/shell')
-rw-r--r-- | src/mongo/shell/mongo.js | 25 | ||||
-rw-r--r-- | src/mongo/shell/query.js | 19 | ||||
-rw-r--r-- | src/mongo/shell/shell_options.cpp | 16 | ||||
-rw-r--r-- | src/mongo/shell/shell_options.h | 5 | ||||
-rw-r--r-- | src/mongo/shell/shell_utils.cpp | 5 | ||||
-rw-r--r-- | src/mongo/shell/utils.js | 5 |
6 files changed, 73 insertions, 2 deletions
diff --git a/src/mongo/shell/mongo.js b/src/mongo/shell/mongo.js index 4b359a67f81..c5384166677 100644 --- a/src/mongo/shell/mongo.js +++ b/src/mongo/shell/mongo.js @@ -252,7 +252,32 @@ Mongo.prototype.writeMode = function() { return this._writeMode; }; +// +// Whether to use find command versus OP_QUERY style find. +// + +Mongo.prototype.useFindCommand = function() { + return (this.readMode() === "commands"); +} + +Mongo.prototype.readMode = function() { + if ("_readMode" in this) { + // We already have determined our read mode. Just return it. + return this._readMode; + } + // Determine read mode based on shell params. + // + // TODO: Detect what to use based on wire protocol version. + if (_readMode) { + this._readMode = _readMode(); + } + else { + this._readMode = "compatibility"; + } + + return this._readMode; +} // // Write Concern can be set at the connection level, and is used for all write operations unless diff --git a/src/mongo/shell/query.js b/src/mongo/shell/query.js index b2290be74ff..06d4fa7b3b5 100644 --- a/src/mongo/shell/query.js +++ b/src/mongo/shell/query.js @@ -80,8 +80,25 @@ DBQuery.prototype._checkModify = function(){ DBQuery.prototype._exec = function(){ if ( ! this._cursor ){ assert.eq( 0 , this._numReturned ); - this._cursor = this._mongo.find( this._ns , this._query , this._fields , this._limit , this._skip , this._batchSize , this._options ); this._cursorSeen = 0; + + if (this._mongo.useFindCommand() && this._collection.getName().indexOf("$cmd") !== 0) { + // Run the query as a find command. Since runCommand() is implemented by running + // a findOne() against the $cmd collection, we have to make sure that we don't try + // to run a find command against the $cmd collection. + var findCmd = this._convertToCommand(); + var cmdRes = this._db.runCommand(findCmd); + this._cursor = new DBCommandCursor(this._mongo, cmdRes); + } + else { + this._cursor = this._mongo.find(this._ns, + this._query, + this._fields, + this._limit, + this._skip, + this._batchSize, + this._options); + } } return this._cursor; } diff --git a/src/mongo/shell/shell_options.cpp b/src/mongo/shell/shell_options.cpp index 8d026fec88b..979e5cc8625 100644 --- a/src/mongo/shell/shell_options.cpp +++ b/src/mongo/shell/shell_options.cpp @@ -142,6 +142,12 @@ namespace mongo { "mode to determine how writes are done:" " commands, compatibility, legacy").hidden(); + options->addOptionChaining("readMode", + "readMode", + moe::String, + "mode to determine how .find() queries are done:" + " commands, compatibility").hidden(); + return Status::OK(); } @@ -260,6 +266,16 @@ namespace mongo { } shellGlobalParams.writeMode = mode; } + if (params.count("readMode")) { + std::string mode = params["readMode"].as<string>(); + if (mode != "commands" && mode != "compatibility") { + throw MsgAssertionException(17397, + mongoutils::str::stream() + << "Unknown readMode option: '" << mode + << "'. Valid modes are: {commands, compatibility}"); + } + shellGlobalParams.readMode = mode; + } /* This is a bit confusing, here are the rules: * diff --git a/src/mongo/shell/shell_options.h b/src/mongo/shell/shell_options.h index 6278ea34da7..bfaedfdf59c 100644 --- a/src/mongo/shell/shell_options.h +++ b/src/mongo/shell/shell_options.h @@ -66,9 +66,12 @@ namespace mongo { bool useWriteCommandsDefault; std::string writeMode; + std::string readMode; + ShellGlobalParams() : autoKillOp(false), useWriteCommandsDefault(true), - writeMode("commands") { + writeMode("commands"), + readMode("compatibility") { } }; diff --git a/src/mongo/shell/shell_utils.cpp b/src/mongo/shell/shell_utils.cpp index 77cf1103f1d..d1382a42f01 100644 --- a/src/mongo/shell/shell_utils.cpp +++ b/src/mongo/shell/shell_utils.cpp @@ -214,6 +214,10 @@ namespace mongo { return BSON("" << shellGlobalParams.writeMode); } + BSONObj readMode(const BSONObj&, void*) { + return BSON("" << shellGlobalParams.readMode); + } + BSONObj interpreterVersion(const BSONObj& a, void* data) { uassert( 16453, "interpreterVersion accepts no arguments", a.nFields() == 0 ); return BSON( "" << globalScriptEngine->getInterpreterVersionString() ); @@ -243,6 +247,7 @@ namespace mongo { // Need to define this method before JSFiles::utils is executed. scope.injectNative("_useWriteCommandsDefault", useWriteCommandsDefault); scope.injectNative("_writeMode", writeMode); + scope.injectNative("_readMode", readMode); scope.externalSetup(); mongo::shell_utils::installShellUtils( scope ); scope.execSetup(JSFiles::servers); diff --git a/src/mongo/shell/utils.js b/src/mongo/shell/utils.js index 7caef381673..e61e3c7f4ff 100644 --- a/src/mongo/shell/utils.js +++ b/src/mongo/shell/utils.js @@ -362,6 +362,11 @@ if (typeof(_writeMode) == 'undefined') { _writeMode = function() { return "commands"; }; }; +if (typeof(_readMode) == 'undefined') { + // This is for cases when the v8 engine is used other than the mongo shell, like map reduce. + _readMode = function() { return "compatibility"; }; +}; + shellPrintHelper = function (x) { if (typeof (x) == "undefined") { // Make sure that we have a db var before we use it |