Skip to content

Commit 88e02a4

Browse files
authored
feat(NODE-4184)!: don't throw on aggregate with write concern and explain (#4718)
1 parent 25de3df commit 88e02a4

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

src/operations/aggregate.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,6 @@ export class AggregateOperation extends CommandOperation<CursorResponse> {
8080
delete this.options.writeConcern;
8181
}
8282

83-
if (this.explain && this.writeConcern) {
84-
throw new MongoInvalidArgumentError(
85-
'Option "explain" cannot be used on an aggregate call with writeConcern'
86-
);
87-
}
88-
8983
if (options?.cursor != null && typeof options.cursor !== 'object') {
9084
throw new MongoInvalidArgumentError('Cursor options must be an object');
9185
}

test/integration/crud/aggregation.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect } from 'chai';
22

3-
import { MongoInvalidArgumentError } from '../../../src/error';
3+
import { MongoInvalidArgumentError, MongoServerError } from '../../../src/error';
44
import { type MongoClient } from '../../../src/mongo_client';
55
import { filterForCommands } from '../shared';
66

@@ -601,7 +601,7 @@ describe('Aggregation', function () {
601601
.toArray()
602602
.catch(error => error);
603603

604-
expect(error).to.be.instanceOf(MongoInvalidArgumentError);
604+
expect(error).to.be.instanceOf(MongoServerError);
605605
});
606606

607607
it('should fail if you try to use explain flag with { writeConcern: { j: true } }', async function () {
@@ -615,7 +615,7 @@ describe('Aggregation', function () {
615615
.toArray()
616616
.catch(error => error);
617617

618-
expect(error).to.be.instanceOf(MongoInvalidArgumentError);
618+
expect(error).to.be.instanceOf(MongoServerError);
619619
});
620620

621621
it('should ensure MaxTimeMS is correctly passed down into command execution when using a cursor', async function () {
Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
'use strict';
1+
import { expect } from 'chai';
22

3-
const { expect } = require('chai');
4-
const { AggregateOperation } = require('../../../src/operations/aggregate');
3+
import { AggregateOperation } from '../../../src/operations/aggregate';
4+
import { MongoDBNamespace, WriteConcern } from '../../mongodb';
55

66
describe('AggregateOperation', function () {
7-
const db = 'test';
7+
const ns = new MongoDBNamespace('test', 'coll');
88

99
describe('#constructor', function () {
1010
context('when out is in the options', function () {
11-
const operation = new AggregateOperation(db, [], { out: 'test', dbName: db });
11+
const operation = new AggregateOperation(ns, [], { out: 'test', dbName: ns.db });
1212

1313
it('sets hasWriteStage to true', function () {
1414
expect(operation.hasWriteStage).to.be.true;
1515
});
1616
});
1717

1818
context('when $out is the last stage', function () {
19-
const operation = new AggregateOperation(db, [{ $out: 'test' }], { dbName: db });
19+
const operation = new AggregateOperation(ns, [{ $out: 'test' }], { dbName: ns.db });
2020

2121
it('sets hasWriteStage to true', function () {
2222
expect(operation.hasWriteStage).to.be.true;
2323
});
2424
});
2525

2626
context('when $out is not the last stage', function () {
27-
const operation = new AggregateOperation(db, [{ $out: 'test' }, { $project: { name: 1 } }], {
28-
dbName: db
27+
const operation = new AggregateOperation(ns, [{ $out: 'test' }, { $project: { name: 1 } }], {
28+
dbName: ns.db
2929
});
3030

3131
it('sets hasWriteStage to false', function () {
@@ -34,7 +34,9 @@ describe('AggregateOperation', function () {
3434
});
3535

3636
context('when $merge is the last stage', function () {
37-
const operation = new AggregateOperation(db, [{ $merge: { into: 'test' } }], { dbName: db });
37+
const operation = new AggregateOperation(ns, [{ $merge: { into: 'test' } }], {
38+
dbName: ns.db
39+
});
3840

3941
it('sets hasWriteStage to true', function () {
4042
expect(operation.hasWriteStage).to.be.true;
@@ -43,9 +45,9 @@ describe('AggregateOperation', function () {
4345

4446
context('when $merge is not the last stage', function () {
4547
const operation = new AggregateOperation(
46-
db,
48+
ns,
4749
[{ $merge: { into: 'test' } }, { $project: { name: 1 } }],
48-
{ dbName: db }
50+
{ dbName: ns.db }
4951
);
5052

5153
it('sets hasWriteStage to false', function () {
@@ -54,19 +56,33 @@ describe('AggregateOperation', function () {
5456
});
5557

5658
context('when no writable stages in empty pipeline', function () {
57-
const operation = new AggregateOperation(db, [], { dbName: db });
59+
const operation = new AggregateOperation(ns, [], { dbName: ns.db });
5860

5961
it('sets hasWriteStage to false', function () {
6062
expect(operation.hasWriteStage).to.be.false;
6163
});
6264
});
6365

6466
context('when no writable stages', function () {
65-
const operation = new AggregateOperation(db, [{ $project: { name: 1 } }], { dbName: db });
67+
const operation = new AggregateOperation(ns, [{ $project: { name: 1 } }], { dbName: ns });
6668

6769
it('sets hasWriteStage to false', function () {
6870
expect(operation.hasWriteStage).to.be.false;
6971
});
7072
});
73+
74+
context('when explain is set', function () {
75+
context('when writeConcern is set', function () {
76+
const operation = new AggregateOperation(ns, [], {
77+
dbName: ns.db,
78+
explain: true,
79+
writeConcern: WriteConcern.fromOptions({ wtimeoutMS: 1000 })
80+
});
81+
82+
it('does not raise an error', function () {
83+
expect(operation.explain).to.exist;
84+
});
85+
});
86+
});
7187
});
7288
});

0 commit comments

Comments
 (0)