Created
October 23, 2015 13:42
-
-
Save egorvas/d75bc9e2f0454ca821f0 to your computer and use it in GitHub Desktop.
DB Migration: Update Post Types Counts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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