Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/cli-repl/test/e2e-direct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,39 @@ 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();
await shell.executeLine('use admin');
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
Expand Down
5 changes: 4 additions & 1 deletion packages/shell-api/src/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down