diff --git a/packages/i18n/src/locales/en_US.ts b/packages/i18n/src/locales/en_US.ts index 4106b5d79b..0977a48ccb 100644 --- a/packages/i18n/src/locales/en_US.ts +++ b/packages/i18n/src/locales/en_US.ts @@ -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.' diff --git a/packages/shell-api/src/mongo.spec.ts b/packages/shell-api/src/mongo.spec.ts index c695a988ee..c9b4b1869d 100644 --- a/packages/shell-api/src/mongo.spec.ts +++ b/packages/shell-api/src/mongo.spec.ts @@ -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 .'); + }); + + it('throws if name is empty', () => { + expect(() => { + mongo.getCollection(''); + }).to.throw('Collection must be of the format .'); + }); + + it('throws if name starts with dot', () => { + expect(() => { + mongo.getCollection('.coll'); + }).to.throw('Collection must be of the format .'); + }); + }); }); describe('integration', () => { diff --git a/packages/shell-api/src/mongo.ts b/packages/shell-api/src/mongo.ts index e2f00bd7e7..57e3a01f86 100644 --- a/packages/shell-api/src/mongo.ts +++ b/packages/shell-api/src/mongo.ts @@ -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'; @@ -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(/^(?[^.]+)\.(?.+)$/)?.groups ?? {}; + if (!db || !coll) { + throw new MongoshInvalidInputError('Collection must be of the format .', CommonErrors.InvalidArgument); + } + return this._getDb(db).getCollection(coll); + } + use(db: string): string { assertArgsDefinedType([db], ['string'], 'Mongo.use'); this._internalState.messageBus.emit('mongosh:use', { db });