A very simple example is enough to give you an idea of how this command works. Let's say that management wants a revenue report for 2019 with a sum for each country. First, we need to define the JavaScript map function. This function emits the country code and total price for each document in the bookings collection:
map_func = function () { \
emit(this.property.propertyAddr.country, this.totalPrice); }
Next, we define the reduce function. This function accepts the two values that are emitted, and produces a sum of the total prices paid aggregated by country:
reduce_func = function (key, prices) { return Array.sum(prices); }
Next, we define the document used as a query filter, as well as the target collection into which the resulting information is written:
query_doc = {"bookingInfo.paymentStatus":"confirmed", "bookingInfo.arrivalDate":/^2019/}
output_to = "country_totals"
We are now ready to execute...