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
5 changes: 5 additions & 0 deletions packages/i18n/src/locales/en_US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,11 @@ const translations: Catalog = {
link: 'https://docs.mongodb.com/manual/reference/method/Mongo.startSession/',
description: 'Starts a session for the connection.'
},
getCollection: {
link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getCollection',
description: 'Returns the specified Collection of the Mongo object.',
example: 'const collection = db.getMongo().getCollection("databaseName.collectionName")'
},
getDB: {
link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getDB',
description: 'Returns the specified Database of the Mongo object.'
Expand Down
33 changes: 33 additions & 0 deletions packages/shell-api/src/mongo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,39 @@ describe('Mongo', () => {
expect.fail('Failed to throw');
});
});
describe('getCollection', () => {
it('returns a collection for the database', async() => {
const coll = mongo.getCollection('db1.coll');
expect(coll).to.be.instanceOf(Collection);
expect(coll._name).to.equal('coll');
expect(coll._database._name).to.equal('db1');
});

it('returns a collection for the database with multiple .', async() => {
const coll = mongo.getCollection('db1.coll.subcoll');
expect(coll).to.be.instanceOf(Collection);
expect(coll._name).to.equal('coll.subcoll');
expect(coll._database._name).to.equal('db1');
});

it('throws if name is not a valid connection string', () => {
expect(() => {
mongo.getCollection('db');
}).to.throw('Collection must be of the format <db>.<collection>');
});

it('throws if name is empty', () => {
expect(() => {
mongo.getCollection('');
}).to.throw('Collection must be of the format <db>.<collection>');
});

it('throws if name starts with dot', () => {
expect(() => {
mongo.getCollection('.coll');
}).to.throw('Collection must be of the format <db>.<collection>');
});
});
});

describe('integration', () => {
Expand Down
11 changes: 11 additions & 0 deletions packages/shell-api/src/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
ServerApi,
ServerApiVersionId
} from '@mongosh/service-provider-core';
import type Collection from './collection';
import Database from './database';
import ShellInternalState from './shell-internal-state';
import { CommandResult } from './result';
Expand Down Expand Up @@ -194,6 +195,16 @@ export default class Mongo extends ShellApiClass {
return this._getDb(db);
}

@returnType('Collection')
getCollection(name: string): Collection {
assertArgsDefinedType([name], ['string']);
const { db, coll } = name.match(/^(?<db>[^.]+)\.(?<coll>.+)$/)?.groups ?? {};
if (!db || !coll) {
throw new MongoshInvalidInputError('Collection must be of the format <db>.<collection>', CommonErrors.InvalidArgument);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message is so much better than the old one 👏

}
return this._getDb(db).getCollection(coll);
}

use(db: string): string {
assertArgsDefinedType([db], ['string'], 'Mongo.use');
this._internalState.messageBus.emit('mongosh:use', { db });
Expand Down