如何在mongodb中表示线程注释(以及评论投票)的数据? [英] How to represent the data for threaded comments(along with comment voting) in mongodb?

查看:202
本文介绍了如何在mongodb中表示线程注释(以及评论投票)的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的博客,我想结合自己的评论系统,而不依赖于默认的wordpress评论系统。我需要注释模块来使用 mongodb 而不是 mysql ,该模块需要支持以下内容:

For my blog, I want to incorporate my own commenting system without relying on the default wordpress commenting system. I need the comments module to utilize mongodb instead of mysql and the module needs support for the following:


  1. 有意见的评论

  2. 评论投票

  3. 需要针对整个博客的每个评论作者聚合评论投票。 / li>
  1. Threaded comments
  2. Comment Voting
  3. The votes on comments need to be aggregated per each comment author for the entire blog.

在这个上下文中,什么是最好的方式来表示mongodb中的数据?

In this context, What is best way to represent the data in mongodb?

推荐答案

只需按照您希望的方式存储您的博客上的评论。你想要线程/嵌套注释?然后以嵌套的方式存储它们:

Just store the comments as you want them represented on your blog. You want threaded/nested comments? Then store them in a nested fashion:

postId: {
  comments: [
    {
      id: "47cc67093475061e3d95369d" // ObjectId
      title: "Title of comment",
      body: "Comment body",
      timestamp: 123456789,
      author: "authorIdentifier",
      upVotes: 11,
      downVotes: 2,
      comments: [
        {
          id: "58ab67093475061e3d95a684"
          title: "Nested comment",
          body: "Hello, this is a nested/threaded comment",
          timestamp: 123456789,
          author: "authorIdentifier",
          upVotes: 11,
          downVotes: 2,
          comments: [
            // More nested comments
          ]
        }
      ]
    },
    {
      // Another top-level comment
    }
  ]
}

postId 指的是评论所属的博客帖子,并被用作MongoDB中的键(或 _id )文件。每个评论都有一个独特的 id ,以便对个人评论进行投票或评论。

The postId refers to the blog post to which the comments belong and has been used as the key (or _id in MongoDB) of the document. Each comment has a unique id, in order to vote or comment on individual comments.

获取聚合投票你需要在这些行的地方编写map-reduce函数:

To get the aggregated votes, you'll need to write map-reduce functions somewhere along these lines:

function map() {
  mapRecursive(this.comments)
}

function mapRecursive(comments) {
  comments.forEach(
    function (c) {
      emit(comment.author, { upVotes: c.upVotes, downVotes: c.downVotes });
      mapRecursive(c.comments);
    }
  );
}

function reduce(key, values) {
  var upVotes = 0;
  var downVotes = 0;

  values.forEach(
    function(votes) {
      upVotes += votes.upVotes;
      downVotes += votes.downVotes;
    }
  );

  return { upVotes: upVotes, downVotes: downVotes };
}

我没有测试这些功能,他们不检查 null 值。这取决于你:)

I haven't tested these functions and they don't check for null values either. That's up to you :)

这篇关于如何在mongodb中表示线程注释(以及评论投票)的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆