@@ -855,42 +855,42 @@ int _main( int argc, char* argv[], char **envp ) {
855855
856856 if ( params.count ( " password" ) && password.empty () )
857857 password = mongo::askPassword ();
858+ }
858859
859- // Construct the authentication-related code to execute on shell startup.
860- //
861- // This constructs and immediately executes an anonymous function, to avoid
862- // the shell's default behavior of printing statement results to the console.
863- //
864- // It constructs a statement of the following form:
865- //
866- // (function() {
867- // // Set default authentication mechanism and, maybe, authenticate.
868- // }())
869- stringstream authStringStream;
870- authStringStream << " (function() { " << endl;
871- if ( !authenticationMechanism.empty () ) {
872- authStringStream << " DB.prototype._defaultAuthenticationMechanism = \" " <<
873- authenticationMechanism << " \" ;" << endl;
874- }
860+ // Construct the authentication-related code to execute on shell startup.
861+ //
862+ // This constructs and immediately executes an anonymous function, to avoid
863+ // the shell's default behavior of printing statement results to the console.
864+ //
865+ // It constructs a statement of the following form:
866+ //
867+ // (function() {
868+ // // Set default authentication mechanism and, maybe, authenticate.
869+ // }())
870+ stringstream authStringStream;
871+ authStringStream << " (function() { " << endl;
872+ if ( !authenticationMechanism.empty () ) {
873+ authStringStream << " DB.prototype._defaultAuthenticationMechanism = \" " <<
874+ authenticationMechanism << " \" ;" << endl;
875+ }
875876
876- if ( username.size () ) {
877- authStringStream << " var username = \" " << username << " \" ;" << endl;
878- authStringStream << " var password = \" " << password << " \" ;" << endl;
879- if (authenticationDatabase.empty ()) {
880- authStringStream << " var authDb = db;" << endl;
881- }
882- else {
883- authStringStream << " var authDb = db.getSiblingDB(\" " << authenticationDatabase <<
884- " \" );" << endl;
885- }
886- authStringStream << " authDb._authOrThrow({ " <<
887- saslCommandPrincipalFieldName << " : username, " <<
888- saslCommandPasswordFieldName << " : password });" << endl;
877+ if (!nodb && username.size ()) {
878+ authStringStream << " var username = \" " << username << " \" ;" << endl;
879+ authStringStream << " var password = \" " << password << " \" ;" << endl;
880+ if (authenticationDatabase.empty ()) {
881+ authStringStream << " var authDb = db;" << endl;
889882 }
890- authStringStream << " }())" ;
891-
892- mongo::shell_utils::_dbAuth = authStringStream.str ();
883+ else {
884+ authStringStream << " var authDb = db.getSiblingDB(\" " << authenticationDatabase <<
885+ " \" );" << endl;
886+ }
887+ authStringStream << " authDb._authOrThrow({ " <<
888+ saslCommandPrincipalFieldName << " : username, " <<
889+ saslCommandPasswordFieldName << " : password });" << endl;
893890 }
891+ authStringStream << " }())" ;
892+ mongo::shell_utils::_dbAuth = authStringStream.str ();
893+
894894
895895 mongo::ScriptEngine::setConnectCallback ( mongo::shell_utils::onConnect );
896896 mongo::ScriptEngine::setup ();
0 commit comments