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 1 commit
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
11 changes: 11 additions & 0 deletions packages/shell-api/src/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@ describe('Database', function () {
serviceProviderCursor = stubInterface<ServiceProviderAggCursor>();
});

it('throws if the given argument is not an array', async function () {
let caughtError: MongoshInvalidInputError | undefined;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
await database.aggregate({} as any).catch((err) => {
caughtError = err;
});
expect(caughtError?.message).contains(
'Aggregate pipeline argument must be an array'
);
});

it('calls serviceProvider.aggregateDb with pipleline and options', async function () {
await database.aggregate([{ $piplelineStage: {} }], { options: true });

Expand Down
9 changes: 9 additions & 0 deletions packages/shell-api/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,14 @@ export default class Database extends ShellApiWithMongoClass {
);
}
assertArgsDefinedType([pipeline], [true], 'Database.aggregate');

if (!Array.isArray(pipeline)) {
throw new MongoshInvalidInputError(
'Aggregate pipeline argument must be an array',
CommonErrors.InvalidArgument
);
}

this._emitDatabaseApiCall('aggregate', { options, pipeline });

const { aggOptions, dbOptions, explain } = adaptAggregateOptions(options);
Expand Down Expand Up @@ -1429,6 +1437,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