Skip to content

Instantly share code, notes, and snippets.

@egorvas
Created October 23, 2015 13:42
Show Gist options
  • Select an option

  • Save egorvas/d75bc9e2f0454ca821f0 to your computer and use it in GitHub Desktop.

Select an option

Save egorvas/d75bc9e2f0454ca821f0 to your computer and use it in GitHub Desktop.
DB Migration: Update Post Types Counts
var businesses = db.getCollection('businesses').find({});
businesses.map(function(business) {
var posts = db.getCollection('posts').find({_business: business._id});
var postTypesCount = {
idea: 0, offer: 0, news: 0, problem: 0, question: 0, review: 0
};
posts.forEach(function(post) {
postTypesCount[post.type]++;
})
return db.getCollection('businesses').update({_id: business._id}, {$set: {
post_types_count: postTypesCount
}})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment