- map keep all members,change members
- filter keep some members,unchanged
- map and filter combined (2 passes if not lazy implemented) keep some members,change members
Reduce is more general,we can do the above and more.
We can also carry any argument with us as state.
map and filter can only see the current member
When we do array operations we need to use a fast function,because it is expected to run many times.
As being said on Collections intro MQL doesn't provide a fast operator to add at the end of an array.
This means that we can't reduce an array to an array in a fast way.
(conj uses $concat and if we make an array with 500+ members its very slow)
- map and filter combination if possible (for cases when we want less members and changed members)
- use cMQL reduce-array operator
cMQL provides a fast reduce solution using $look-up(join with 1 dummy collection of 1 document), $facet,and accumulators.
cMQL uses this solution to group fast,but it can be used for arrays also.
Inside its group-by nil,and we can only use the few accumulators operators that we have.
Because of its limitations doesn't really solve the problem.
Reduce (Array->single value)
Simple and very fast (as long as the function we use is fast)
Simple to use and very fast (as long as the function we use is fast)
The last one the combination of filter and map,can be used as reduce alternative if we want to keep only some members and change them also.
Array->Array(reduce array operator)
Simple but very slow if :myarray > 500 members,dont use in that case.
Array->Array (reduce cMQL operator)
Very fast no matter how big is the array,but pipeline stage not operator
=> results are added to the root document => i can later move it anywhere with assoc-in ,which is very fast
reduce is done using lookup with pipeline(with the dummy collection),facet,unwind,group its very fast and with combination of get-in and assoc-in of cMQL we can do it to any nested array,and then move it back into its place
I can use any MQL accumulator,here i use 3 accumulators.
We can use reduce-array,for nested arrays also,and i can put the results back to the original locations.
The reduce-array is fast,the get-in is very fast,and the assoc-in is very fast.