Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Threaded comments and backboneJS

Threaded comments and backboneJS


Currently, I am implementing a blog application with nodejs/expressjs and mongoDB (+mongoose) as a database. It works fine so far but now I am trying to implement nested/threaded comments.

As I read about the different ways how to store such comments in mongoDB I will store each comment in a separate document to retain flexiblity.

In contrast to store all comments embedded in a thread I will retrieve my comments in a unordered form from the DB with references to each parent... Materialized paths would be an alternative but then, indexing can be problematic due to their length.

I am planning to to the actual sorting client side when rendering the comments - a good idea?

I read about Backbonejs (relational) and ask myself if this is a suitable tool that can help to implement it.

Problem courtesy of: JoeFrizz


Yes storing the comments as separate documents in Mongo would be fine. Keyed lookups are generally fast in Mongo so maintaining a separate normalized structure of Threads and Comments should work.

# Create thread:
db.threads.insert({ id: 123, name: "My thread title..." })

# Add comment:
db.comments.insert({ id: 1, threadId: 123, text: "My comment text..." })

# Find comments in thread by order of insertion (assuming ids are sequential):
db.comments.find({ threadId: 123 }).sort({ id: 1 })

Backbone is a good framework if you're building a single page app. Additionally I'd suggest looking at Backbone Marionette. It's an extension that makes a lot of cool things possible (good documentation too).

Solution courtesy of: Robertus Mensae


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Threaded comments and backboneJS


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription