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:
- Threaded comments
- Comment Voting
- The votes on comments need to be aggregated per each comment author for the entire blog.
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:
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 uniqueid
, in order to vote or comment on individual comments.To get the aggregated votes, you’ll need to write map-reduce functions somewhere along these lines:
I haven’t tested these functions and they don’t check for
null
values either. That’s up to you 🙂How about:
A single collection for all comments. Each comment object holds a reference to a parent comment object for threading and of course the a reference to the parent blog post.
Each comment object also has numeric vote count that can be updated using mongo’s atomic updates.
Each specific vote by a user would then be a reference to the comment’s id in the user’s object directly.