Skip to content

Commit bbf2a73

Browse files
authored
feat(shell-api): add Mongo.getCollection() MONGOSH-620 (#889)
1 parent 720728d commit bbf2a73

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

packages/i18n/src/locales/en_US.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,6 +1659,11 @@ const translations: Catalog = {
16591659
link: 'https://docs.mongodb.com/manual/reference/method/Mongo.startSession/',
16601660
description: 'Starts a session for the connection.'
16611661
},
1662+
getCollection: {
1663+
link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getCollection',
1664+
description: 'Returns the specified Collection of the Mongo object.',
1665+
example: 'const collection = db.getMongo().getCollection("databaseName.collectionName")'
1666+
},
16621667
getDB: {
16631668
link: 'https://docs.mongodb.com/manual/reference/method/Mongo.getDB',
16641669
description: 'Returns the specified Database of the Mongo object.'

packages/shell-api/src/mongo.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,39 @@ describe('Mongo', () => {
609609
expect.fail('Failed to throw');
610610
});
611611
});
612+
describe('getCollection', () => {
613+
it('returns a collection for the database', async() => {
614+
const coll = mongo.getCollection('db1.coll');
615+
expect(coll).to.be.instanceOf(Collection);
616+
expect(coll._name).to.equal('coll');
617+
expect(coll._database._name).to.equal('db1');
618+
});
619+
620+
it('returns a collection for the database with multiple .', async() => {
621+
const coll = mongo.getCollection('db1.coll.subcoll');
622+
expect(coll).to.be.instanceOf(Collection);
623+
expect(coll._name).to.equal('coll.subcoll');
624+
expect(coll._database._name).to.equal('db1');
625+
});
626+
627+
it('throws if name is not a valid connection string', () => {
628+
expect(() => {
629+
mongo.getCollection('db');
630+
}).to.throw('Collection must be of the format <db>.<collection>');
631+
});
632+
633+
it('throws if name is empty', () => {
634+
expect(() => {
635+
mongo.getCollection('');
636+
}).to.throw('Collection must be of the format <db>.<collection>');
637+
});
638+
639+
it('throws if name starts with dot', () => {
640+
expect(() => {
641+
mongo.getCollection('.coll');
642+
}).to.throw('Collection must be of the format <db>.<collection>');
643+
});
644+
});
612645
});
613646

614647
describe('integration', () => {

packages/shell-api/src/mongo.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
ServerApi,
3737
ServerApiVersionId
3838
} from '@mongosh/service-provider-core';
39+
import type Collection from './collection';
3940
import Database from './database';
4041
import ShellInternalState from './shell-internal-state';
4142
import { CommandResult } from './result';
@@ -194,6 +195,16 @@ export default class Mongo extends ShellApiClass {
194195
return this._getDb(db);
195196
}
196197

198+
@returnType('Collection')
199+
getCollection(name: string): Collection {
200+
assertArgsDefinedType([name], ['string']);
201+
const { db, coll } = name.match(/^(?<db>[^.]+)\.(?<coll>.+)$/)?.groups ?? {};
202+
if (!db || !coll) {
203+
throw new MongoshInvalidInputError('Collection must be of the format <db>.<collection>', CommonErrors.InvalidArgument);
204+
}
205+
return this._getDb(db).getCollection(coll);
206+
}
207+
197208
use(db: string): string {
198209
assertArgsDefinedType([db], ['string'], 'Mongo.use');
199210
this._internalState.messageBus.emit('mongosh:use', { db });

0 commit comments

Comments
 (0)