I am using Ruby code to calculate sum from the array returned by Mongoid.
But maybe using Map/Reduce can be faster, except I don't see any docs for Map Reduce on mongoid.org and Google for
map reduce site:mongoid.org
doesn't give any result either. (or using MapReduce
or Map/Reduce
)
There are docs on MongoDB's site
map reduce site:mongodb.org
but need to use Map Reduce with Mongoid as well.
In MongoDB, map-reduce is a data processing programming model that helps to perform operations on large data sets and produce aggregated results. MongoDB provides the mapReduce() function to perform the map-reduce operations. This function has two main functions, i.e., map function and reduce function.
The map function may optionally call emit(key,value) any number of times to create an output document associating key with value . In MongoDB 4.2 and earlier, a single emit can only hold half of MongoDB's maximum BSON document size. MongoDB removes this restriction starting in version 4.4.
Map-reduce is a common pattern when working with Big Data – it's a way to extract info from a huge dataset. But now, starting with version 2.2, MongoDB includes a new feature called Aggregation framework. Functionality-wise, Aggregation is equivalent to map-reduce but, on paper, it promises to be much faster.
You can use map reduce with Mongoid just as you could through the Ruby driver directly:
# Post is a Mongoid model...
Post.collection.map_reduce(map_function, reduce_function, options)
For some examples of doing map reduce in the Ruby driver, see this blog post by Kyle Banker (maintainer of the Ruby MongoDB driver).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With