Permalink
Browse files

修身 topic_collect 的计数

  • Loading branch information...
1 parent 9b9bfe7 commit b692e514a0340071c0c45756b27dd6199f589efb @alsotang alsotang committed Apr 20, 2016
Showing with 61 additions and 0 deletions.
  1. +61 −0 bin/fix_topic_collect_count.js
@@ -0,0 +1,61 @@
+var TopicCollect = require('../models').TopicCollect;
+var UserModel = require('../models').User;
+var TopicModel = require('../models').Topic
+
+// 修复用户的topic_collect计数
+TopicCollect.aggregate(
+ [{
+ "$group" :
+ {
+ _id : {user_id: "$user_id"},
+ count : { $sum : 1}
+ }
+ }], function (err, result) {
+ result.forEach(function (row) {
+ var userId = row._id.user_id;
+ var count = row.count;
+
+ UserModel.findOne({
+ _id: userId
+ }, function (err, user) {
+
+ if (!user) {
+ return;
+ }
+
+ user.collect_topic_count = count;
+ user.save(function () {
+ console.log(user.loginname, count)
+ });
+ })
+ })
+ })
+
+ // 修复帖子的topic_collect计数
+ TopicCollect.aggregate(
+ [{
+ "$group" :
+ {
+ _id : {topic_id: "$topic_id"},
+ count : { $sum : 1}
+ }
+ }], function (err, result) {
+ result.forEach(function (row) {
+ var topic_id = row._id.topic_id;
+ var count = row.count;
+
+ TopicModel.findOne({
+ _id: topic_id
+ }, function (err, topic) {
+
+ if (!topic) {
+ return;
+ }
+
+ topic.collect_topic_count = count;
+ topic.save(function () {
+ console.log(topic.id, count)
+ });
+ })
+ })
+ })

0 comments on commit b692e51

Please sign in to comment.