Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mongoose = require('mongoose');
- var uniqueValidator = require('mongoose-unique-validator');
- var slug = require('slug'); // package for auto-creating URL slugs
- var User = mongoose.model('User');
- //...
- // this is really slow for a large collection of users, but it gives an accurate result.
- // iterate over all users and count how many have this article in their favorite list
- ArticleSchema.methods.updateFavoriteCount = function() {
- var article = this;
- return User.count({favorites: {$in: [article._id]}}).then(function(count){
- article.favoritesCount = count;
- return article.save();
- });
- };
- mongoose.model('Article', ArticleSchema);
Advertisement
Add Comment
Please, Sign In to add comment