[SERVER-287] Finalizer for group() Created: 10/Sep/09  Updated: 12/Jul/16  Resolved: 17/Sep/09

Status: Closed
Project: Core Server
Component/s: Usability
Affects Version/s: None
Fix Version/s: 1.1.1

Type: Improvement Priority: Major - P3
Reporter: Mathias Stearn Assignee: Mathias Stearn
Resolution: Done Votes: 1
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

It would be nice if there was an extra finalizer option on the group command. Right now it is difficult to implement an average function using just group(). While this could be done in the calling language, in some cases it is nice to be able to do everything in one place in javascript.

Example

db.coll.group(
{key:"date"
,initial:

{ct:0, sum:0}

,reduce: function(doc, out)

{out.ct++; out.sum += doc.val}

,final: function(result)

{result.sum = result.ct ? result.sum/result.ct : 0}

})

Ideally, if the final function returned a non-undefined result it would replace that position in the output, similar to a map function.



 Comments   
Comment by Mathias Stearn [ 17/Sep/09 ]

Note: final has been renamed 'finalize' to avoid conflicting with JavaScript reserved words. See jstests/group3.js for examples.

Generated at Thu Feb 08 02:53:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.