Skip to content

feat(NODE-4924)!: remove mapReduce collection helper #3511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions etc/notes/CHANGES_5.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,43 @@ The following is a detailed collection of the changes in the major v5 release of

## Changes

### `Collection.mapReduce()` helper removed

The `mapReduce` helper has been removed from the `Collection` class. The `mapReduce` operation has been
deprecated in favor of the aggregation pipeline since MongoDB server version 5.0. It is recommended
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/)).

If the `mapReduce` command must be used, the `Db.command()` helper can be used to run the raw
`mapReduce` command.

```typescript
// using the Collection.mapReduce helper in <4.x drivers
const collection = db.collection('my-collection');

await collection.mapReduce(
function() { emit(this.user_id, 1); },
function(k, vals) { return 1 },
{
out: 'inline',
readConcern: 'majority'
}
)

// manually running the command using `db.command()`
const command = {
mapReduce: 'my-collection',
map: 'function() { emit(this.user_id, 1); }',
reduce: 'function(k,vals) { return 1; }',
out: 'inline',
readConcern: 'majority'
}

await db.command(command);
```

**Note** When using the `Db.command()` helper, all `mapReduce` options should be specified
on the raw command object and should not be passed through the options object.

### `AddUserOptions.digestPassword` removed

The `digestPassword` option has been removed from the add user helper.
Expand Down
79 changes: 0 additions & 79 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,6 @@ import {
InsertOneResult
} from './operations/insert';
import { IsCappedOperation } from './operations/is_capped';
import {
MapFunction,
MapReduceOperation,
MapReduceOptions,
ReduceFunction
} from './operations/map_reduce';
import type { Hint, OperationOptions } from './operations/operation';
import { OptionsOperation } from './operations/options_operation';
import { RenameOperation, RenameOptions } from './operations/rename';
Expand Down Expand Up @@ -1524,79 +1518,6 @@ export class Collection<TSchema extends Document = Document> {
return new ChangeStream<TLocal, TChange>(this, pipeline, resolveOptions(this, options));
}

/**
* Run Map Reduce across a collection. Be aware that the inline option for out will return an array of results not a collection.
*
* @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.
* @param map - The mapping function.
* @param reduce - The reduce function.
* @param options - Optional settings for the command
* @param callback - An optional callback, a Promise will be returned if none is provided
*/
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>
): Promise<Document | Document[]>;
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>,
options: MapReduceOptions<TKey, TValue>
): Promise<Document | Document[]>;
/** @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 */
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>,
callback: Callback<Document | Document[]>
): void;
/** @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 */
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>,
options: MapReduceOptions<TKey, TValue>,
callback: Callback<Document | Document[]>
): void;
mapReduce<TKey = any, TValue = any>(
map: string | MapFunction<TSchema>,
reduce: string | ReduceFunction<TKey, TValue>,
options?: MapReduceOptions<TKey, TValue> | Callback<Document | Document[]>,
callback?: Callback<Document | Document[]>
): Promise<Document | Document[]> | void {
emitWarningOnce(
'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.'
);
if ('function' === typeof options) (callback = options), (options = {});
// Out must always be defined (make sure we don't break weirdly on pre 1.8+ servers)
// TODO NODE-3339: Figure out if this is still necessary given we no longer officially support pre-1.8
if (options?.out == null) {
throw new MongoInvalidArgumentError(
'Option "out" must be defined, see mongodb docs for possible values'
);
}

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

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

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

return executeOperation(
this.s.db.s.client,
new MapReduceOperation(
this as TODO_NODE_3286,
map,
reduce,
resolveOptions(this, options) as TODO_NODE_3286
),
callback
);
}

/**
* Initiate an Out of order batch write operation. All operations will be buffered into insert/update/remove commands executed out of order.
*
Expand Down
6 changes: 0 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,6 @@ export type {
export type { InsertManyResult, InsertOneOptions, InsertOneResult } from './operations/insert';
export type { CollectionInfo, ListCollectionsOptions } from './operations/list_collections';
export type { ListDatabasesOptions, ListDatabasesResult } from './operations/list_databases';
export type {
FinalizeFunction,
MapFunction,
MapReduceOptions,
ReduceFunction
} from './operations/map_reduce';
export type { AbstractOperation, Hint, OperationOptions } from './operations/operation';
export type { ProfilingLevelOptions } from './operations/profiling_level';
export type { RemoveUserOptions } from './operations/remove_user';
Expand Down
Loading