diff --git a/packages/shell-api/src/database.spec.ts b/packages/shell-api/src/database.spec.ts index d4fd662e2..a78e97101 100644 --- a/packages/shell-api/src/database.spec.ts +++ b/packages/shell-api/src/database.spec.ts @@ -400,6 +400,16 @@ describe('Database', function () { ); }); + it('supports a single aggregation stage', async function () { + await database.aggregate({ $piplelineStage: {} }, { options: true }); + + expect(serviceProvider.aggregateDb).to.have.been.calledWith( + database._name, + [{ $piplelineStage: {} }], + { options: true } + ); + }); + it('calls serviceProvider.aggregateDb with explicit batchSize', async function () { await database.aggregate([{ $piplelineStage: {} }], { options: true, diff --git a/packages/shell-api/src/database.ts b/packages/shell-api/src/database.ts index 1765c9299..7c6ce4a3b 100644 --- a/packages/shell-api/src/database.ts +++ b/packages/shell-api/src/database.ts @@ -423,7 +423,7 @@ export default class Database extends ShellApiWithMongoClass { @returnType('AggregationCursor') @apiVersions([1]) async aggregate( - pipeline: Document[], + pipelineOrSingleStage: Document | Document[], options?: Document ): Promise { if ('background' in (options ?? {})) { @@ -431,7 +431,12 @@ export default class Database extends ShellApiWithMongoClass { aggregateBackgroundOptionNotSupportedHelp ); } + const pipeline: Document[] = Array.isArray(pipelineOrSingleStage) + ? pipelineOrSingleStage + : [pipelineOrSingleStage]; + assertArgsDefinedType([pipeline], [true], 'Database.aggregate'); + this._emitDatabaseApiCall('aggregate', { options, pipeline }); const { aggOptions, dbOptions, explain } = adaptAggregateOptions(options); @@ -1429,6 +1434,7 @@ export default class Database extends ShellApiWithMongoClass { CommonErrors.CommandFailed ); } + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion for (const cmdDescription of Object.values(result.commands) as Document[]) { if ('slaveOk' in cmdDescription) { cmdDescription.secondaryOk = cmdDescription.slaveOk;