Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shell-api): Add support for running aggregate database with a single stage MONGOSH-1868 #2218

Merged
merged 8 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 5 additions & 2 deletions packages/shell-api/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export default class Collection extends ShellApiWithMongoClass {
async aggregate(
pipeline: Document[],
options: Document & { explain: ExplainVerbosityLike }
): Promise<Document>;
): Promise<AggregationCursor>;
gagik marked this conversation as resolved.
Show resolved Hide resolved
async aggregate(...stages: Document[]): Promise<AggregationCursor>;
@returnsPromise
@returnType('AggregationCursor')
Expand All @@ -191,7 +191,10 @@ export default class Collection extends ShellApiWithMongoClass {
this._database._name,
this._name,
pipeline,
{ ...(await this._database._baseOptions()), ...aggOptions },
{
...(await this._database._baseOptions()),
...aggOptions,
},
dbOptions
);
const cursor = new AggregationCursor(this._mongo, providerCursor);
Expand Down
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
16 changes: 15 additions & 1 deletion packages/shell-api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ export default class Database extends ShellApiWithMongoClass {
return await this._runAdminCommand(cmd, {});
}

async aggregate(
singleStage: Document,
options?: Document
): Promise<AggregationCursor>;
async aggregate(
pipeline: Document[],
options?: Document
): Promise<AggregationCursor>;
gagik marked this conversation as resolved.
Show resolved Hide resolved
/**
* Run an aggregation against the db.
*
Expand All @@ -423,15 +431,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 +1442,7 @@ export default class Database extends ShellApiWithMongoClass {
CommonErrors.CommandFailed
);
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
gagik marked this conversation as resolved.
Show resolved Hide resolved
for (const cmdDescription of Object.values(result.commands) as Document[]) {
if ('slaveOk' in cmdDescription) {
cmdDescription.secondaryOk = cmdDescription.slaveOk;
Expand Down
Loading