Skip to content

Commit dc6e2d6

Browse files
authored
fix(NODE-2883): Aggregate Operation should not require parent parameter (#2918)
1 parent 967c193 commit dc6e2d6

File tree

6 files changed

+16
-35
lines changed

6 files changed

+16
-35
lines changed

src/change_stream.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,11 @@ export class ChangeStreamCursor<TSchema extends Document = Document> extends Abs
489489
}
490490

491491
_initialize(session: ClientSession, callback: Callback<ExecutionResult>): void {
492-
const aggregateOperation = new AggregateOperation(
493-
{ s: { namespace: this.namespace } },
494-
this.pipeline,
495-
{
496-
...this.cursorOptions,
497-
...this.options,
498-
session
499-
}
500-
);
492+
const aggregateOperation = new AggregateOperation(this.namespace, this.pipeline, {
493+
...this.cursorOptions,
494+
...this.options,
495+
session
496+
});
501497

502498
executeOperation(this.topology, aggregateOperation, (err, response) => {
503499
if (err || response == null) {

src/collection.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,6 @@ export class Collection<TSchema extends Document = Document> {
13871387
}
13881388

13891389
return new AggregationCursor(
1390-
this as TODO_NODE_3286,
13911390
getTopology(this),
13921391
this.s.namespace,
13931392
pipeline,

src/cursor/aggregation_cursor.ts

+3-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ import type { Sort } from '../sort';
77
import type { Topology } from '../sdam/topology';
88
import type { Callback, MongoDBNamespace } from '../utils';
99
import type { ClientSession } from '../sessions';
10-
import type { OperationParent } from '../operations/command';
1110
import type { AbstractCursorOptions } from './abstract_cursor';
1211
import type { ExplainVerbosityLike } from '../explain';
1312
import type { Projection } from '../mongo_types';
1413

1514
/** @public */
1615
export interface AggregationCursorOptions extends AbstractCursorOptions, AggregateOptions {}
1716

18-
/** @internal */
19-
const kParent = Symbol('parent');
2017
/** @internal */
2118
const kPipeline = Symbol('pipeline');
2219
/** @internal */
@@ -30,24 +27,20 @@ const kOptions = Symbol('options');
3027
* @public
3128
*/
3229
export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchema> {
33-
/** @internal */
34-
[kParent]: OperationParent; // TODO: NODE-2883
3530
/** @internal */
3631
[kPipeline]: Document[];
3732
/** @internal */
3833
[kOptions]: AggregateOptions;
3934

4035
/** @internal */
4136
constructor(
42-
parent: OperationParent,
4337
topology: Topology,
4438
namespace: MongoDBNamespace,
4539
pipeline: Document[] = [],
4640
options: AggregateOptions = {}
4741
) {
4842
super(topology, namespace, options);
4943

50-
this[kParent] = parent;
5144
this[kPipeline] = pipeline;
5245
this[kOptions] = options;
5346
}
@@ -59,7 +52,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
5952
clone(): AggregationCursor<TSchema> {
6053
const clonedOptions = mergeOptions({}, this[kOptions]);
6154
delete clonedOptions.session;
62-
return new AggregationCursor(this[kParent], this.topology, this.namespace, this[kPipeline], {
55+
return new AggregationCursor(this.topology, this.namespace, this[kPipeline], {
6356
...clonedOptions
6457
});
6558
}
@@ -70,7 +63,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
7063

7164
/** @internal */
7265
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
73-
const aggregateOperation = new AggregateOperation(this[kParent], this[kPipeline], {
66+
const aggregateOperation = new AggregateOperation(this.namespace, this[kPipeline], {
7467
...this[kOptions],
7568
...this.cursorOptions,
7669
session
@@ -97,7 +90,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
9790

9891
return executeOperation(
9992
this.topology,
100-
new AggregateOperation(this[kParent], this[kPipeline], {
93+
new AggregateOperation(this.namespace, this[kPipeline], {
10194
...this[kOptions], // NOTE: order matters here, we may need to refine this
10295
...this.cursorOptions,
10396
explain: verbosity

src/db.ts

-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ export class Db {
301301
}
302302

303303
return new AggregationCursor(
304-
this,
305304
getTopology(this),
306305
this.s.namespace,
307306
pipeline,

src/operations/aggregate.ts

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
import {
2-
CommandOperation,
3-
CommandOperationOptions,
4-
OperationParent,
5-
CollationOptions
6-
} from './command';
1+
import { CommandOperation, CommandOperationOptions, CollationOptions } from './command';
72
import { ReadPreference } from '../read_preference';
83
import { MongoInvalidArgumentError } from '../error';
9-
import { maxWireVersion } from '../utils';
4+
import { maxWireVersion, MongoDBNamespace } from '../utils';
105
import { Aspect, defineAspects, Hint } from './operation';
116
import type { Callback } from '../utils';
127
import type { Document } from '../bson';
@@ -47,14 +42,13 @@ export class AggregateOperation<T = Document> extends CommandOperation<T> {
4742
pipeline: Document[];
4843
hasWriteStage: boolean;
4944

50-
constructor(parent: OperationParent, pipeline: Document[], options?: AggregateOptions) {
51-
super(parent, options);
45+
constructor(ns: MongoDBNamespace, pipeline: Document[], options?: AggregateOptions) {
46+
super(undefined, { ...options, dbName: ns.db });
5247

5348
this.options = options ?? {};
54-
this.target =
55-
parent.s.namespace && parent.s.namespace.collection
56-
? parent.s.namespace.collection
57-
: DB_AGGREGATE_COLLECTION;
49+
50+
// Covers when ns.collection is null, undefined or the empty string, use DB_AGGREGATE_COLLECTION
51+
this.target = ns.collection || DB_AGGREGATE_COLLECTION;
5852

5953
this.pipeline = pipeline;
6054

src/operations/count_documents.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class CountDocumentsOperation extends AggregateOperation<number> {
2929

3030
pipeline.push({ $group: { _id: 1, n: { $sum: 1 } } });
3131

32-
super(collection, pipeline, options);
32+
super(collection.s.namespace, pipeline, options);
3333
}
3434

3535
execute(server: Server, session: ClientSession, callback: Callback<number>): void {

0 commit comments

Comments
 (0)