Skip to content

Commit

Permalink
fix(shell-api): Add support for running aggregate database with a sin…
Browse files Browse the repository at this point in the history
…gle stage MONGOSH-1868 (#2218)

Co-authored-by: Anna Henningsen <[email protected]>
  • Loading branch information
gagik and addaleax authored Oct 18, 2024
1 parent a08940b commit af04244
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/shell-api/src/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion packages/shell-api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,20 @@ export default class Database extends ShellApiWithMongoClass {
@returnType('AggregationCursor')
@apiVersions([1])
async aggregate(
pipeline: Document[],
pipelineOrSingleStage: Document | Document[],
options?: Document
): Promise<AggregationCursor> {
if ('background' in (options ?? {})) {
await this._instanceState.printWarning(
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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit af04244

Please sign in to comment.