如何根据if语句从mongoDB中删除post?

How to delete post from mongoDB based on if statement?

在 Meteor Framework 中工作,我有 Posts.Collection 个 post,你可以投票赞成或反对,我想在投票得分为 -5 时删除 post。

Template.postList.helpers({
 post: function() {
  if (post.score < 5) {
   Posts.remove(post._id);
     };
    }
});

分数通过服务器更新。我还需要通过服务器删除 posts 吗?

好吧,你可以在你投反对票的事件上这样做,它会自动删除..

Template.example.events({
  'click #downVoteButton':function(e,t){
    //on the update.
    Posts.update({_id:this.id},{$inc{score:-1}},function(err,result){
     if(!err){
       var post = Posts.findOne({_id:this._id})
       if(post.score >= 5){
         Posts.remove({_id:this._id})
        }
      }
    })
  }
})

但是如果你不相信downvotes,并且你想在删除他后检查post

你可以在一些迷你管理员后端做一个查找(帮助),在那里你会得到所有超过 5 个反对票的 post,你检查反对票是否有效,然后你继续删除它们.

或者为此使用 Hooks

即时或即时查找 after.insert,有很多选项可以做到这一点,但我更喜欢迷你管理员的想法