As I dive into understanding the types for the mapReduce function, I come across the types defined for mapReduceOptions
(although I am aware that this is deprecated).
export interface MapReduceOptions<TKey = ObjectId, TValue = Document>
extends CommandOperationOptions {
/** Sets the output target for the map reduce job. */
out?: 'inline' | { inline: 1 } | { replace: string } | { merge: string } | { reduce: string };
/** Query filter object. */
query?: Document;
/** Sorts the input objects using this key. Useful for optimization, like sorting by the emit key for fewer reduces. */
sort?: Sort;
/** Number of objects to return from collection. */
limit?: number;
/** Keep temporary data. */
keeptemp?: boolean;
/** Finalize function. */
finalize?: FinalizeFunction<TKey, TValue> | string;
/** Can pass in variables that can be access from map/reduce/finalize. */
scope?: Document;
/** It is possible to make the execution stay in JS. Provided in MongoDB \> 2.0.X. */
jsMode?: boolean;
/** Provide statistics on job execution time. */
verbose?: boolean;
/** Allow driver to bypass schema validation in MongoDB 3.2 or higher. */
bypassDocumentValidation?: boolean;
}
Reference in the code here:https://github.com/mongodb/node-mongodb-native/blob/4.1/src/operations/map_reduce.ts#L50
Now, based on the examples I have reviewed, the usage of out
seems to be consistent as a required parameter. So, first question - why is it marked as optional
? And secondly, should it not be a string
instead of specifying options like inline
?
out?: 'inline' | { inline: 1 } | { replace: string } | { merge: string } | { reduce: string };
Wouldn't it be more intuitive to use it like this?
mapReduce(
function(){ emit(this.Name,1)},
function(key, values) {return Array.sum(values)},
{query:{Marks:{$gt:70}},out: 'Name_Total'}).