Skip to content

Commit

Permalink
refactor!: remove deprecated Collection.group helper
Browse files Browse the repository at this point in the history
The group command was deprecated in MongoDB 3.4 and removed in
MongoDB 3.6, this PR removes it from the driver.

NODE-1487
  • Loading branch information
emadum committed Nov 10, 2020
1 parent 2f23dd7 commit cf5c865
Show file tree
Hide file tree
Showing 7 changed files with 1 addition and 409 deletions.
76 changes: 0 additions & 76 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ import { RenameOperation, RenameOptions } from './operations/rename';
import { ReplaceOneOperation, ReplaceOptions } from './operations/replace_one';
import { CollStatsOperation, CollStatsOptions } from './operations/stats';
import { executeOperation } from './operations/execute_operation';
import { EvalGroupOperation, GroupOperation } from './operations/group';
import type { Db } from './db';
import type { OperationOptions, Hint } from './operations/operation';
import type { IndexInformationOptions } from './operations/common_functions';
Expand Down Expand Up @@ -1510,76 +1509,6 @@ export class Collection implements OperationParent {
);
}

/**
* Run a group command across a collection
*
* @deprecated MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework.
* @param keys - An object, array or function expressing the keys to group by.
* @param condition - An optional condition that must be true for a row to be considered.
* @param initial - Initial value of the aggregation counter object.
* @param reduce - The reduce function aggregates (reduces) the objects iterated
* @param finalize - An optional function to be run on each item in the result set just before the item is returned.
* @param command - Specify if you wish to run using the internal group command or using eval, default is true.
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
group(
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
keys: any,
condition: Document,
initial: Document,
// TODO: Use labeled tuples when api-extractor supports TS 4.0
...args: [/*reduce?:*/ any, /*finalize?:*/ any, /*command?:*/ any, /*callback?:*/ Callback]
): Promise<Document> | void {
const callback = typeof args[args.length - 1] === 'function' ? args.pop() : undefined;
let reduce = args.length ? args.shift() : undefined;
let finalize = args.length ? args.shift() : undefined;
let command = args.length ? args.shift() : undefined;
let options = args.length ? args.shift() || {} : {};

// Make sure we are backward compatible
if (!(typeof finalize === 'function')) {
command = finalize;
finalize = undefined;
}

if (
!Array.isArray(keys) &&
keys instanceof Object &&
typeof keys !== 'function' &&
!(keys._bsontype === 'Code')
) {
keys = Object.keys(keys);
}

if (typeof reduce === 'function') {
reduce = reduce.toString();
}

if (typeof finalize === 'function') {
finalize = finalize.toString();
}

// Set up the command as default
command = command == null ? true : command;

options = resolveOptions(this, options);

if (command == null) {
return executeOperation(
getTopology(this),
new EvalGroupOperation(this, keys, condition, initial, reduce, finalize, options),
callback
);
}

return executeOperation(
getTopology(this),
new GroupOperation(this, keys, condition, initial, reduce, finalize, options),
callback
);
}

/**
* Find and modify a document.
*
Expand Down Expand Up @@ -1694,8 +1623,3 @@ Collection.prototype.findAndRemove = deprecate(
Collection.prototype.findAndRemove,
'collection.findAndRemove is deprecated. Use findOneAndDelete instead.'
);

Collection.prototype.group = deprecate(
Collection.prototype.group,
'MongoDB 3.6 or higher no longer supports the group command. We recommend rewriting using the aggregation framework.'
);
101 changes: 0 additions & 101 deletions src/operations/group.ts

This file was deleted.

9 changes: 1 addition & 8 deletions src/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,14 +717,7 @@ class ServerSessionPool {
// TODO: this should be codified in command construction
// @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.rst#read-concern
function commandSupportsReadConcern(command: Document, options?: Document): boolean {
if (
command.aggregate ||
command.count ||
command.distinct ||
command.find ||
command.geoNear ||
command.group
) {
if (command.aggregate || command.count || command.distinct || command.find || command.geoNear) {
return true;
}

Expand Down
44 changes: 0 additions & 44 deletions test/functional/collations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,50 +166,6 @@ describe('Collation', function () {
}
});

it('Successfully pass through collation to group command', {
metadata: { requires: { generators: true, topology: 'single', mongodb: '<=4.1.0' } },

test: function () {
const configuration = this.configuration;
const client = configuration.newClient(`mongodb://${testContext.server.uri()}/test`);
const primary = [Object.assign({}, mock.DEFAULT_ISMASTER)];

let commandResult;
testContext.server.setMessageHandler(request => {
var doc = request.document;
if (doc.ismaster) {
request.reply(primary[0]);
} else if (doc.group) {
commandResult = doc;
request.reply({ ok: 1 });
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});

return client.connect().then(() => {
const db = client.db(configuration.db);

return db
.collection('test')
.group(
[],
{ a: { $gt: 1 } },
{ count: 0 },
'function (obj, prev) { prev.count++; }',
'function (obj, prev) { prev.count++; }',
true,
{ collation: { caseLevel: true } }
)
.then(() => {
expect(commandResult).to.have.property('collation');
expect(commandResult.collation).to.eql({ caseLevel: true });
return client.close();
});
});
}
});

it('Successfully pass through collation to mapReduce command', {
metadata: { requires: { generators: true, topology: 'single' } },

Expand Down
118 changes: 0 additions & 118 deletions test/functional/mapreduce.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,6 @@ describe('MapReduce', function () {
return setupDatabase(this.configuration, ['outputCollectionDb']);
});

it('shouldCorrectlyExecuteGroupFunctionWithFinalizeFunction', {
metadata: {
requires: {
mongodb: '<=4.1.0',
topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger']
}
},

test: function (done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
client.connect(function (err, client) {
var db = client.db(configuration.db);
db.createCollection('test_group2', function (err, collection) {
collection.group(
[],
{},
{ count: 0 },
'function (obj, prev) { prev.count++; }',
true,
function (err, results) {
test.deepEqual([], results);

// Trigger some inserts
collection.insert(
[{ a: 2 }, { b: 5, a: 0 }, { a: 1 }, { c: 2, a: 0 }],
configuration.writeConcernMax(),
function (err) {
expect(err).to.not.exist;
collection.group(
[],
{},
{ count: 0, running_average: 0 },
function (doc, out) {
out.count++;
out.running_average += doc.a;
},
function (out) {
out.average = out.running_average / out.count;
},
true,
function (err, results) {
test.equal(3, results[0].running_average);
test.equal(0.75, results[0].average);
client.close(done);
}
);
}
);
}
);
});
});
}
});

/**
* Mapreduce tests
*/
Expand Down Expand Up @@ -386,68 +330,6 @@ describe('MapReduce', function () {
}
});

it('shouldCorrectlyReturnNestedKeys', {
metadata: {
requires: {
mongodb: '<=4.1.0', // Because of use of `group` command
topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger']
}
},

test: function (done) {
var configuration = this.configuration;
var client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
client.connect(function (err, client) {
var db = client.db(configuration.db);
var start = new Date().setTime(new Date().getTime() - 10000);
var end = new Date().setTime(new Date().getTime() + 10000);

var keys = {
'data.lastname': true
};

var condition = {
'data.date': {
$gte: start,
$lte: end
}
};

condition = {};

var initial = {
count: 0
};

var reduce = function (doc, output) {
output.count++;
};

// Execute the group
db.createCollection('data', function (err, collection) {
collection.insert(
{
data: {
lastname: 'smith',
date: new Date()
}
},
configuration.writeConcernMax(),
function (err) {
expect(err).to.not.exist;
// Execute the group
collection.group(keys, condition, initial, reduce, true, function (err, r) {
test.equal(1, r[0].count);
test.equal('smith', r[0]['data.lastname']);
client.close(done);
});
}
);
});
});
}
});

/**
* Mapreduce tests
*/
Expand Down

0 comments on commit cf5c865

Please sign in to comment.