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
9 changes: 9 additions & 0 deletions packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,15 @@ const translations: Catalog = {
link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getDB',
description: 'Returns the specified Database of the Mongo object.'
},
getDBs: {
link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getDBs',
description: 'Returns information about all databases. Uses the listDatabases command.'
},
getDBNames: {
link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getDBs',
description: 'Returns an array of all database names. Uses the listDatabases command.',
example: 'db.getMongo().getDBNames().map(name => db.getSiblingDB(name).getCollectionNames())'
},
connect: {
link: 'https://docs.mongodb.com/manual/reference/method/connect',
description: 'Creates a connection to a MongoDB instance and returns the reference to the database.'
Expand Down
12 changes: 12 additions & 0 deletions packages/shell-api/src/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,18 @@ describe('Shell API (integration)', function() {
expect(internalState.mongos).to.deep.equal([mongo]);
});
});
describe('getDBs', () => {
it('lists all databases', async() => {
const result = await mongo.getDBs();
expect(result.ok).to.equal(1);
expect(result.databases.find(db => db.name === 'admin').sizeOnDisk).to.be.a('number');
});
});
describe('getDBNames', () => {
it('lists all database names', async() => {
expect(await mongo.getDBNames()).to.include('admin');
});
});
});
describe('PlanCache', () => {
describe('list', () => {
Expand Down
20 changes: 16 additions & 4 deletions packages/shell-api/src/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,21 @@ export default class Mongo extends ShellApiClass {
return `switched to db ${db}`;
}

async _listDatabases(opts: ListDatabasesOptions = {}): Promise<{name: string, sizeOnDisk: number, empty: boolean}[]> {
async _listDatabases(opts: ListDatabasesOptions = {}): Promise<{ databases: {name: string, sizeOnDisk: number, empty: boolean}[] }> {
const result = await this._serviceProvider.listDatabases('admin', { ...opts });
if (!('databases' in result)) {
const err = new MongoshRuntimeError('Got invalid result from "listDatabases"', CommonErrors.CommandFailed);
this._internalState.messageBus.emit('mongosh:error', err);
throw err;
}
this._cachedDatabaseNames = result.databases.map((db: any) => db.name);
return result.databases;
return result as any;
}

async _getDatabaseNamesForCompletion(): Promise<string[]> {
return await Promise.race([
(async() => {
return (await this._listDatabases({ readPreference: 'primaryPreferred' })).map(db => db.name);
return (await this._listDatabases({ readPreference: 'primaryPreferred' })).databases.map(db => db.name);
})(),
(async() => {
// See the comment in _getCollectionNamesForCompletion/database.ts
Expand All @@ -243,14 +243,26 @@ export default class Mongo extends ShellApiClass {
]);
}

@returnsPromise
async getDBs(options: ListDatabasesOptions = {}): Promise<{ databases: {name: string, sizeOnDisk: number, empty: boolean}[] }> {
this._emitMongoApiCall('getDBs', { options });
return await this._listDatabases(options);
}

@returnsPromise
async getDBNames(options: ListDatabasesOptions = {}): Promise<string[]> {
this._emitMongoApiCall('getDBNames', { options });
return (await this._listDatabases(options)).databases.map(db => db.name);
}

@returnsPromise
async show(cmd: string, arg?: string): Promise<CommandResult> {
this._internalState.messageBus.emit('mongosh:show', { method: `show ${cmd}` });

switch (cmd) {
case 'databases':
case 'dbs':
const result = await this._listDatabases({ readPreference: 'primaryPreferred' });
const result = (await this._listDatabases({ readPreference: 'primaryPreferred' })).databases;
return new CommandResult('ShowDatabasesResult', result);
case 'collections':
case 'tables':
Expand Down