Skip to content

Commit cf5c865

Browse files
authored
refactor!: remove deprecated Collection.group helper
The group command was deprecated in MongoDB 3.4 and removed in MongoDB 3.6, this PR removes it from the driver. NODE-1487
1 parent 2f23dd7 commit cf5c865

File tree

7 files changed

+1
-409
lines changed

7 files changed

+1
-409
lines changed

src/collection.ts

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ import { RenameOperation, RenameOptions } from './operations/rename';
7777
import { ReplaceOneOperation, ReplaceOptions } from './operations/replace_one';
7878
import { CollStatsOperation, CollStatsOptions } from './operations/stats';
7979
import { executeOperation } from './operations/execute_operation';
80-
import { EvalGroupOperation, GroupOperation } from './operations/group';
8180
import type { Db } from './db';
8281
import type { OperationOptions, Hint } from './operations/operation';
8382
import type { IndexInformationOptions } from './operations/common_functions';
@@ -1510,76 +1509,6 @@ export class Collection implements OperationParent {
15101509
);
15111510
}
15121511

1513-
/**
1514-
* Run a group command across a collection
1515-
*
1516-
* @deprecated MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework.
1517-
* @param keys - An object, array or function expressing the keys to group by.
1518-
* @param condition - An optional condition that must be true for a row to be considered.
1519-
* @param initial - Initial value of the aggregation counter object.
1520-
* @param reduce - The reduce function aggregates (reduces) the objects iterated
1521-
* @param finalize - An optional function to be run on each item in the result set just before the item is returned.
1522-
* @param command - Specify if you wish to run using the internal group command or using eval, default is true.
1523-
* @param options - Optional settings for the command
1524-
* @param callback - An optional callback, a Promise will be returned if none is provided
1525-
*/
1526-
group(
1527-
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1528-
keys: any,
1529-
condition: Document,
1530-
initial: Document,
1531-
// TODO: Use labeled tuples when api-extractor supports TS 4.0
1532-
...args: [/*reduce?:*/ any, /*finalize?:*/ any, /*command?:*/ any, /*callback?:*/ Callback]
1533-
): Promise<Document> | void {
1534-
const callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
1535-
let reduce = args.length ? args.shift() : undefined;
1536-
let finalize = args.length ? args.shift() : undefined;
1537-
let command = args.length ? args.shift() : undefined;
1538-
let options = args.length ? args.shift() || {} : {};
1539-
1540-
// Make sure we are backward compatible
1541-
if (!(typeof finalize === 'function')) {
1542-
command = finalize;
1543-
finalize = undefined;
1544-
}
1545-
1546-
if (
1547-
!Array.isArray(keys) &&
1548-
keys instanceof Object &&
1549-
typeof keys !== 'function' &&
1550-
!(keys._bsontype === 'Code')
1551-
) {
1552-
keys = Object.keys(keys);
1553-
}
1554-
1555-
if (typeof reduce === 'function') {
1556-
reduce = reduce.toString();
1557-
}
1558-
1559-
if (typeof finalize === 'function') {
1560-
finalize = finalize.toString();
1561-
}
1562-
1563-
// Set up the command as default
1564-
command = command == null ? true : command;
1565-
1566-
options = resolveOptions(this, options);
1567-
1568-
if (command == null) {
1569-
return executeOperation(
1570-
getTopology(this),
1571-
new EvalGroupOperation(this, keys, condition, initial, reduce, finalize, options),
1572-
callback
1573-
);
1574-
}
1575-
1576-
return executeOperation(
1577-
getTopology(this),
1578-
new GroupOperation(this, keys, condition, initial, reduce, finalize, options),
1579-
callback
1580-
);
1581-
}
1582-
15831512
/**
15841513
* Find and modify a document.
15851514
*
@@ -1694,8 +1623,3 @@ Collection.prototype.findAndRemove = deprecate(
16941623
Collection.prototype.findAndRemove,
16951624
'collection.findAndRemove is deprecated. Use findOneAndDelete instead.'
16961625
);
1697-
1698-
Collection.prototype.group = deprecate(
1699-
Collection.prototype.group,
1700-
'MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework.'
1701-
);

src/operations/group.ts

Lines changed: 0 additions & 101 deletions
This file was deleted.

src/sessions.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -717,14 +717,7 @@ class ServerSessionPool {
717717
// TODO: this should be codified in command construction
718718
// @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.rst#read-concern
719719
function commandSupportsReadConcern(command: Document, options?: Document): boolean {
720-
if (
721-
command.aggregate ||
722-
command.count ||
723-
command.distinct ||
724-
command.find ||
725-
command.geoNear ||
726-
command.group
727-
) {
720+
if (command.aggregate || command.count || command.distinct || command.find || command.geoNear) {
728721
return true;
729722
}
730723

test/functional/collations.test.js

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -166,50 +166,6 @@ describe('Collation', function () {
166166
}
167167
});
168168

169-
it('Successfully pass through collation to group command', {
170-
metadata: { requires: { generators: true, topology: 'single', mongodb: '<=4.1.0' } },
171-
172-
test: function () {
173-
const configuration = this.configuration;
174-
const client = configuration.newClient(`mongodb://${testContext.server.uri()}/test`);
175-
const primary = [Object.assign({}, mock.DEFAULT_ISMASTER)];
176-
177-
let commandResult;
178-
testContext.server.setMessageHandler(request => {
179-
var doc = request.document;
180-
if (doc.ismaster) {
181-
request.reply(primary[0]);
182-
} else if (doc.group) {
183-
commandResult = doc;
184-
request.reply({ ok: 1 });
185-
} else if (doc.endSessions) {
186-
request.reply({ ok: 1 });
187-
}
188-
});
189-
190-
return client.connect().then(() => {
191-
const db = client.db(configuration.db);
192-
193-
return db
194-
.collection('test')
195-
.group(
196-
[],
197-
{ a: { $gt: 1 } },
198-
{ count: 0 },
199-
'function (obj, prev) { prev.count++; }',
200-
'function (obj, prev) { prev.count++; }',
201-
true,
202-
{ collation: { caseLevel: true } }
203-
)
204-
.then(() => {
205-
expect(commandResult).to.have.property('collation');
206-
expect(commandResult.collation).to.eql({ caseLevel: true });
207-
return client.close();
208-
});
209-
});
210-
}
211-
});
212-
213169
it('Successfully pass through collation to mapReduce command', {
214170
metadata: { requires: { generators: true, topology: 'single' } },
215171

test/functional/mapreduce.test.js

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,6 @@ describe('MapReduce', function () {
99
return setupDatabase(this.configuration, ['outputCollectionDb']);
1010
});
1111

12-
it('shouldCorrectlyExecuteGroupFunctionWithFinalizeFunction', {
13-
metadata: {
14-
requires: {
15-
mongodb: '<=4.1.0',
16-
topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger']
17-
}
18-
},
19-
20-
test: function (done) {
21-
var configuration = this.configuration;
22-
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
23-
client.connect(function (err, client) {
24-
var db = client.db(configuration.db);
25-
db.createCollection('test_group2', function (err, collection) {
26-
collection.group(
27-
[],
28-
{},
29-
{ count: 0 },
30-
'function (obj, prev) { prev.count++; }',
31-
true,
32-
function (err, results) {
33-
test.deepEqual([], results);
34-
35-
// Trigger some inserts
36-
collection.insert(
37-
[{ a: 2 }, { b: 5, a: 0 }, { a: 1 }, { c: 2, a: 0 }],
38-
configuration.writeConcernMax(),
39-
function (err) {
40-
expect(err).to.not.exist;
41-
collection.group(
42-
[],
43-
{},
44-
{ count: 0, running_average: 0 },
45-
function (doc, out) {
46-
out.count++;
47-
out.running_average += doc.a;
48-
},
49-
function (out) {
50-
out.average = out.running_average / out.count;
51-
},
52-
true,
53-
function (err, results) {
54-
test.equal(3, results[0].running_average);
55-
test.equal(0.75, results[0].average);
56-
client.close(done);
57-
}
58-
);
59-
}
60-
);
61-
}
62-
);
63-
});
64-
});
65-
}
66-
});
67-
6812
/**
6913
* Mapreduce tests
7014
*/
@@ -386,68 +330,6 @@ describe('MapReduce', function () {
386330
}
387331
});
388332

389-
it('shouldCorrectlyReturnNestedKeys', {
390-
metadata: {
391-
requires: {
392-
mongodb: '<=4.1.0', // Because of use of `group` command
393-
topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger']
394-
}
395-
},
396-
397-
test: function (done) {
398-
var configuration = this.configuration;
399-
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
400-
client.connect(function (err, client) {
401-
var db = client.db(configuration.db);
402-
var start = new Date().setTime(new Date().getTime() - 10000);
403-
var end = new Date().setTime(new Date().getTime() + 10000);
404-
405-
var keys = {
406-
'data.lastname': true
407-
};
408-
409-
var condition = {
410-
'data.date': {
411-
$gte: start,
412-
$lte: end
413-
}
414-
};
415-
416-
condition = {};
417-
418-
var initial = {
419-
count: 0
420-
};
421-
422-
var reduce = function (doc, output) {
423-
output.count++;
424-
};
425-
426-
// Execute the group
427-
db.createCollection('data', function (err, collection) {
428-
collection.insert(
429-
{
430-
data: {
431-
lastname: 'smith',
432-
date: new Date()
433-
}
434-
},
435-
configuration.writeConcernMax(),
436-
function (err) {
437-
expect(err).to.not.exist;
438-
// Execute the group
439-
collection.group(keys, condition, initial, reduce, true, function (err, r) {
440-
test.equal(1, r[0].count);
441-
test.equal('smith', r[0]['data.lastname']);
442-
client.close(done);
443-
});
444-
}
445-
);
446-
});
447-
});
448-
}
449-
});
450-
451333
/**
452334
* Mapreduce tests
453335
*/

0 commit comments

Comments
 (0)