meteor-reactive-aggregate
meteor-reactive-aggregate copied to clipboard
Add transform option
Hey. My pipeline requires that the resulting documents are transformed before being added to the client-side collection. In order to do this I added a transform
option.
Let me know what you think. If you want to add it I'll add some documentation.
Hey, the changes look good to me. Could you give an example what you use this for and why it can't be solved within the aggregation?
I am not sure that this is the only way to do what I want, but it sure is easy to understand. I have a query that aggregates data for a graph:
$group: {_id: {year: {$year: date}, week: {$week: date}}, total: {$sum: {$multiply: ['$price', '$amount']}}}
Meteor complains about my key not being an ObjectID, String, or Number, and changing my query causes MongoDB to complain about my grouping props not being part of the _id.
I suspect I can reorganize the result using the pipeline, but I have not yet found an example of doing this, and my own fiddling around hasn't produced any functional results.
Having the ability to have a transform function is a very easy to understand way of dealing with this.
Alternatively, some more documentation on ReactiveAggregate and how to use the pipeline to achieve these results would be really useful. Even better would be to just have a function in the pipeline (maybe you can do this now? - it isn't obvious if it is currently possible).
I have managed to solve my immediate problem using the pipeline to convert my dates to strings, then split the _id into multiple values, then concat my id pieces into a single string.
While this works for this specific use case, I can see a lot of valid use cases where this doesn't work since Mongo does not appear to provide a way to convert non-strings other than date into strings (why the hell not?), and concat does not accept non-string values (again why the hell not?).
After half a day of research, I would suggest that some documentation in the readme.md regarding how to use the pipeline to aggregate data and split apart the id, and $concat the id would probably answer a lot of user questions with just a few minutes of reading/writing. I would also love to see mention of $lookup to perform an outer join on the data since it solves related problems that would otherwise require ugly post processing.
@wcrisman is there a reason your value has to be in the _id
? you could quite easily $group
by that id and have the data in another field in your aggregation pipeline:
{ $group: {
_id: { $dateToString: { date: '$date', format: '%Y%M%d' } },
year: { $year: '$date' },
week: { $week: '$date' },
total: { $sum: { $multiply: ['$price', '$amount'] } }
}},