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

How do I get average? #140

Closed
evgenyboxer opened this issue Nov 1, 2016 · 1 comment
Closed

How do I get average? #140

evgenyboxer opened this issue Nov 1, 2016 · 1 comment

Comments

@evgenyboxer
Copy link

evgenyboxer commented Nov 1, 2016

Hey,

I'm trying to figure out how to calculate the average rating for a review.
my review model has a rating field, its an array of objects that looks like:

rating: [ { communication: 5, value: 4, accuracy: 5 }, ... ] 

How do I calculate the average of communication/value/accuracy using feathers-mongoose?

Here's my solution not using mongoose (Its in the after hooks)

(hook, next) => {
   const data = hook.result.data;
    hook.result.avg.communication = mean(data.map(entry => entry.rating.communication));
    ...
@daffl
Copy link
Member

daffl commented Nov 2, 2016

The hook solution looks fine to me (definitely easier than trying to figure out the right MongoDB query for this, it is database agnostic and probably not any slower). As long as you make sure the Mongoose model gets converted to an object before modifying the result I don't see a problem with this.

@daffl daffl closed this as completed Nov 3, 2016
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

No branches or pull requests

2 participants