Generates a popularity value of collection based on user defined fields & factors
All definitions should be made server side only.
// Sets up a 'authorPopularity' field on each author
Authors.createPopularityContest([{key: "likes", weight: 2}, {key: "comments", weight: 3}, {key: "followers", weight: 1}], 'authorPopularity');
The example creates a new field 'authorPopularity' to Authors collection with value: likes * 2 + comments * 3 + followers * 1. The weigth values must be integer values. The popularity field will be automatically updated whenever given key values change.
key | value |
---|---|
_id | 1 |
name | author1 |
likes | 10 |
comments | 20 |
followers | 30 |
authorPopularity | 110 |
key | value |
---|---|
_id | 1 |
name | author2 |
likes | 10 |
comments | 30 |
followers | 20 |
authorPopularity | 130 |
The calculated value helps fetching collections in popularity order.
Authors.find({}, {sort:{authorPopularity:-1}}).fetch()[0].name = author2
Authors.find({}, {sort:{authorPopularity:-1}}).fetch()[1].name = author1
collection.createPopularityContest(lookup, name)
- collection
The collection to store the popularity value on
- Arguments
lookup — Array
Array of objects containing field names and factors on collection to perform the popularity count.
name — String
Specify a custom popularity field name
MIT. (c) Jani Halmetoja