mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-15 14:30:22 +00:00
Merge pull request #34584 from lenartbezek/patch-1
[@types/mongodb] Collection.mapReduce
This commit is contained in:
Vendored
+5
-5
@@ -955,9 +955,9 @@ export interface Collection<TSchema = Default> {
|
||||
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#listIndexes */
|
||||
listIndexes(options?: { batchSize?: number, readPreference?: ReadPreference | string, session?: ClientSession }): CommandCursor;
|
||||
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#mapReduce */
|
||||
mapReduce(map: CollectionMapFunction | string, reduce: CollectionReduceFunction | string, callback: MongoCallback<any>): void;
|
||||
mapReduce(map: CollectionMapFunction | string, reduce: CollectionReduceFunction | string, options?: MapReduceOptions): Promise<any>;
|
||||
mapReduce(map: CollectionMapFunction | string, reduce: CollectionReduceFunction | string, options: MapReduceOptions, callback: MongoCallback<any>): void;
|
||||
mapReduce<TKey, TValue>(map: CollectionMapFunction<TSchema> | string, reduce: CollectionReduceFunction<TKey, TValue> | string, callback: MongoCallback<any>): void;
|
||||
mapReduce<TKey, TValue>(map: CollectionMapFunction<TSchema> | string, reduce: CollectionReduceFunction<TKey, TValue> | string, options?: MapReduceOptions): Promise<any>;
|
||||
mapReduce<TKey, TValue>(map: CollectionMapFunction<TSchema> | string, reduce: CollectionReduceFunction<TKey, TValue> | string, options: MapReduceOptions, callback: MongoCallback<any>): void;
|
||||
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#options */
|
||||
options(options?: { session: ClientSession }): Promise<any>;
|
||||
options(callback: MongoCallback<any>): void;
|
||||
@@ -1663,9 +1663,9 @@ export interface MapReduceOptions {
|
||||
session?: ClientSession;
|
||||
}
|
||||
|
||||
export type CollectionMapFunction = () => void;
|
||||
export type CollectionMapFunction<TSchema> = (this: TSchema) => void;
|
||||
|
||||
export type CollectionReduceFunction = (key: string, values: any) => any;
|
||||
export type CollectionReduceFunction<TKey, TValue> = (key: TKey, values: TValue[]) => TValue;
|
||||
|
||||
/** http://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#~WriteOpResult */
|
||||
export interface WriteOpResult {
|
||||
|
||||
@@ -317,3 +317,29 @@ mongodb.connect(connectionString).then((client) => {
|
||||
runTransactionWithRetry(updateEmployeeInfo, client, session)
|
||||
);
|
||||
});
|
||||
|
||||
// https://docs.mongodb.com/manual/core/map-reduce/
|
||||
|
||||
// Declare emit function to be called inside map function
|
||||
declare function emit(key: any, value: any): void;
|
||||
|
||||
interface ITestMapReduceSchema {
|
||||
cust_id: string;
|
||||
amount: number;
|
||||
status: string;
|
||||
}
|
||||
|
||||
function testCollectionMapFunction(this: ITestMapReduceSchema) {
|
||||
emit(this.cust_id, this.amount);
|
||||
}
|
||||
|
||||
function testCollectionReduceFunction(_key: string, values: number[]): number {
|
||||
return values.reduce((a, v) => a + v, 0);
|
||||
}
|
||||
|
||||
mongodb.connect(connectionString).then((client) => {
|
||||
client.db("test").collection<ITestMapReduceSchema>('test-mapReduce-collection').mapReduce(
|
||||
testCollectionMapFunction,
|
||||
testCollectionReduceFunction
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user