Skip to content

Commit 10d757a

Browse files
feat(NODE-4924)!: remove mapReduce collection helper (#3511)
1 parent c286be7 commit 10d757a

21 files changed

+59
-1520
lines changed

etc/notes/CHANGES_5.0.0.md

+37
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,43 @@ The following is a detailed collection of the changes in the major v5 release of
1616

1717
## Changes
1818

19+
### `Collection.mapReduce()` helper removed
20+
21+
The `mapReduce` helper has been removed from the `Collection` class. The `mapReduce` operation has been
22+
deprecated in favor of the aggregation pipeline since MongoDB server version 5.0. It is recommended
23+
to migrate code that uses `Collection.mapReduce` to use the aggregation pipeline (see [Map-Reduce to Aggregation Pipeline](https://www.mongodb.com/docs/manual/reference/map-reduce-to-aggregation-pipeline/)).
24+
25+
If the `mapReduce` command must be used, the `Db.command()` helper can be used to run the raw
26+
`mapReduce` command.
27+
28+
```typescript
29+
// using the Collection.mapReduce helper in <4.x drivers
30+
const collection = db.collection('my-collection');
31+
32+
await collection.mapReduce(
33+
function() { emit(this.user_id, 1); },
34+
function(k, vals) { return 1 },
35+
{
36+
out: 'inline',
37+
readConcern: 'majority'
38+
}
39+
)
40+
41+
// manually running the command using `db.command()`
42+
const command = {
43+
mapReduce: 'my-collection',
44+
map: 'function() { emit(this.user_id, 1); }',
45+
reduce: 'function(k,vals) { return 1; }',
46+
out: 'inline',
47+
readConcern: 'majority'
48+
}
49+
50+
await db.command(command);
51+
```
52+
53+
**Note** When using the `Db.command()` helper, all `mapReduce` options should be specified
54+
on the raw command object and should not be passed through the options object.
55+
1956
### `AddUserOptions.digestPassword` removed
2057

2158
The `digestPassword` option has been removed from the add user helper.

src/collection.ts

-79
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,6 @@ import {
6868
InsertOneResult
6969
} from './operations/insert';
7070
import { IsCappedOperation } from './operations/is_capped';
71-
import {
72-
MapFunction,
73-
MapReduceOperation,
74-
MapReduceOptions,
75-
ReduceFunction
76-
} from './operations/map_reduce';
7771
import type { Hint, OperationOptions } from './operations/operation';
7872
import { OptionsOperation } from './operations/options_operation';
7973
import { RenameOperation, RenameOptions } from './operations/rename';
@@ -1524,79 +1518,6 @@ export class Collection<TSchema extends Document = Document> {
15241518
return new ChangeStream<TLocal, TChange>(this, pipeline, resolveOptions(this, options));
15251519
}
15261520

1527-
/**
1528-
* Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
1529-
*
1530-
* @deprecated collection.mapReduce is deprecated. Use the aggregation pipeline instead. Visit https://docs.mongodb.com/manual/reference/map-reduce-to-aggregation-pipeline for more information on how to translate map-reduce operations to the aggregation pipeline.
1531-
* @param map - The mapping function.
1532-
* @param reduce - The reduce function.
1533-
* @param options - Optional settings for the command
1534-
* @param callback - An optional callback, a Promise will be returned if none is provided
1535-
*/
1536-
mapReduce<TKey = any, TValue = any>(
1537-
map: string | MapFunction<TSchema>,
1538-
reduce: string | ReduceFunction<TKey, TValue>
1539-
): Promise<Document | Document[]>;
1540-
mapReduce<TKey = any, TValue = any>(
1541-
map: string | MapFunction<TSchema>,
1542-
reduce: string | ReduceFunction<TKey, TValue>,
1543-
options: MapReduceOptions<TKey, TValue>
1544-
): Promise<Document | Document[]>;
1545-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
1546-
mapReduce<TKey = any, TValue = any>(
1547-
map: string | MapFunction<TSchema>,
1548-
reduce: string | ReduceFunction<TKey, TValue>,
1549-
callback: Callback<Document | Document[]>
1550-
): void;
1551-
/** @deprecated Callbacks are deprecated and will be removed in the next major version. See [mongodb-legacy](https://github.com/mongodb-js/nodejs-mongodb-legacy) for migration assistance */
1552-
mapReduce<TKey = any, TValue = any>(
1553-
map: string | MapFunction<TSchema>,
1554-
reduce: string | ReduceFunction<TKey, TValue>,
1555-
options: MapReduceOptions<TKey, TValue>,
1556-
callback: Callback<Document | Document[]>
1557-
): void;
1558-
mapReduce<TKey = any, TValue = any>(
1559-
map: string | MapFunction<TSchema>,
1560-
reduce: string | ReduceFunction<TKey, TValue>,
1561-
options?: MapReduceOptions<TKey, TValue> | Callback<Document | Document[]>,
1562-
callback?: Callback<Document | Document[]>
1563-
): Promise<Document | Document[]> | void {
1564-
emitWarningOnce(
1565-
'collection.mapReduce is deprecated. Use the aggregation pipeline instead. Visit https://docs.mongodb.com/manual/reference/map-reduce-to-aggregation-pipeline for more information on how to translate map-reduce operations to the aggregation pipeline.'
1566-
);
1567-
if ('function' === typeof options) (callback = options), (options = {});
1568-
// Out must always be defined (make sure we don't break weirdly on pre 1.8+ servers)
1569-
// TODO NODE-3339: Figure out if this is still necessary given we no longer officially support pre-1.8
1570-
if (options?.out == null) {
1571-
throw new MongoInvalidArgumentError(
1572-
'Option "out" must be defined, see mongodb docs for possible values'
1573-
);
1574-
}
1575-
1576-
if ('function' === typeof map) {
1577-
map = map.toString();
1578-
}
1579-
1580-
if ('function' === typeof reduce) {
1581-
reduce = reduce.toString();
1582-
}
1583-
1584-
if ('function' === typeof options.finalize) {
1585-
options.finalize = options.finalize.toString();
1586-
}
1587-
1588-
return executeOperation(
1589-
this.s.db.s.client,
1590-
new MapReduceOperation(
1591-
this as TODO_NODE_3286,
1592-
map,
1593-
reduce,
1594-
resolveOptions(this, options) as TODO_NODE_3286
1595-
),
1596-
callback
1597-
);
1598-
}
1599-
16001521
/**
16011522
* Initiate an Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order.
16021523
*

src/index.ts

-6
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,6 @@ export type {
385385
export type { InsertManyResult, InsertOneOptions, InsertOneResult } from './operations/insert';
386386
export type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections';
387387
export type { ListDatabasesOptions, ListDatabasesResult } from './operations/list_databases';
388-
export type {
389-
FinalizeFunction,
390-
MapFunction,
391-
MapReduceOptions,
392-
ReduceFunction
393-
} from './operations/map_reduce';
394388
export type { AbstractOperation, Hint, OperationOptions } from './operations/operation';
395389
export type { ProfilingLevelOptions } from './operations/profiling_level';
396390
export type { RemoveUserOptions } from './operations/remove_user';

0 commit comments

Comments
 (0)