diff --git a/packages/cli-repl/test/e2e-direct.spec.ts b/packages/cli-repl/test/e2e-direct.spec.ts index d1a230a0c1..637ac7c691 100644 --- a/packages/cli-repl/test/e2e-direct.spec.ts +++ b/packages/cli-repl/test/e2e-direct.spec.ts @@ -121,6 +121,31 @@ describe('e2e direct connection', () => { shell.assertContainsOutput("name: 'system.version'"); }); + it('fails to list databases without explicit readPreference', async() => { + const shell = TestShell.start({ args: [`${await rs1.connectionString()}`] }); + await shell.waitForPrompt(); + await shell.executeLine('use admin'); + await shell.executeLine('db.getMongo().getDBs()'); + shell.assertContainsError('MongoServerError: not primary'); + }); + + it('lists databases when readPreference is in the connection string', async() => { + const shell = TestShell.start({ args: [`${await rs1.connectionString()}?readPreference=secondaryPreferred`] }); + await shell.waitForPrompt(); + await shell.executeLine('use admin'); + await shell.executeLine('db.getMongo().getDBs()'); + shell.assertContainsOutput("name: 'admin'"); + }); + + it('lists databases when readPreference is set via Mongo', async() => { + const shell = TestShell.start({ args: [`${await rs1.connectionString()}`] }); + await shell.waitForPrompt(); + await shell.executeLine('use admin'); + await shell.executeLine('db.getMongo().setReadPref("secondaryPreferred")'); + await shell.executeLine('db.getMongo().getDBs()'); + shell.assertContainsOutput("name: 'admin'"); + }); + it('lists collections and dbs using show by default', async() => { const shell = TestShell.start({ args: [`${await rs1.connectionString()}`] }); await shell.waitForPrompt(); @@ -128,6 +153,7 @@ describe('e2e direct connection', () => { expect(await shell.executeLine('show collections')).to.include('system.version'); expect(await shell.executeLine('show dbs')).to.include('admin'); }); + it('autocompletes collection names', async function() { if (process.arch === 's390x') { return this.skip(); // https://jira.mongodb.org/browse/MONGOSH-746 diff --git a/packages/shell-api/src/mongo.ts b/packages/shell-api/src/mongo.ts index ae2775ff99..5fcd655f95 100644 --- a/packages/shell-api/src/mongo.ts +++ b/packages/shell-api/src/mongo.ts @@ -242,7 +242,10 @@ export default class Mongo extends ShellApiClass { } async _listDatabases(opts: ListDatabasesOptions = {}): Promise<{ databases: {name: string, sizeOnDisk: number, empty: boolean}[] }> { - const result = await this._serviceProvider.listDatabases('admin', { ...opts }); + const result = await this._serviceProvider.listDatabases('admin', { + ...this._getExplicitlyRequestedReadPref(), + ...opts + }); if (!('databases' in result)) { const err = new MongoshRuntimeError('Got invalid result from "listDatabases"', CommonErrors.CommandFailed); this._instanceState.messageBus.emit('mongosh:error', err, 'shell-api');