Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transform option #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

TimFletcher
Copy link

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.

@JcBernack
Copy link
Owner

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?

@wcrisman
Copy link

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).

@wcrisman
Copy link

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.

@rkstar
Copy link
Contributor

rkstar commented Jan 26, 2018

@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'] } }
}},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants