Tutorial: Use MongoDB to Store Comments

Leave your reply

Introduction

MongoDB is a popular NoSQL database which is perfectly suited for a task like storing comments on a high-traffic site where availability and speed are a priority. This step-by-step tutorial will walk through the process of using MongoDB to store and display user-generated comments submitted from a form which does not require a login.

Website comments are an excellent use case for MongoDB versus a traditional relational database. The user-entered data (like the username) is erratic, which can cause duplicate record headaches with a relational database. Comments may be submitted quickly and in great volume during high-traffic times, which requires high performance.

The biggest reason to choose NoSQL for this purpose is that comments are not relational. There is no need to store extra information about each comment in multiple tables. The only data which needs to be stored is the username, the comment itself, the timestamp, and (optionally) the ID of the comment that it was in reply to, for sites where comments are displayed in threaded format.

Requirements

  • A Cloud Server running Linux (CentOS 7)
  • MongoDB installed and running.

For more information on installing and using MongoDB see the article Installing and Running MongoDB on a Linux Server

Project Overview

There are many possible approaches for this project. In particular, many people wonder whether they should store each comment as its own document, or store all of the comments as a single document which gets updated with each comment.

Although there are some benefits to storing all comments as a single document, MongoDB best practices are to avoid situations where a document will grow in size over time. Additionally, the larger a document gets, the more difficult it is to store on the disk. This can cause significant read/write performance issues.

For this tutorial we will store each comment as a separate document.

The command to insert a comment into the database is:

db.comments.insert({
    'parent_article_id': [ID of parent article],
    'posted': ISODate("[timestamp]"),
    'author': '[Author's name]',
    'comment_body': '[Text of comment]' })
  • parent_article_id: The ID of the article where the comment was left.
  • author: The author's name (a free-form field, not authenticated).
  • comment_body: The text of the comment.

Create the Database and Insert a Comment

Access the MongoDB command line shell:

mongodb

Create the database comment_tracker for the project:

use comment_tracker

Insert the first comment:

db.comments.insert({
    'parent_article_id': 1,
    'posted': ISODate("2017-03-22T19:53:59.526+0000"),
    'author': 'Alice',
    'comment_body': 'Test comment' })

MongoDB will respond with a message that the record has been inserted:

>     db.comments.insert({
...         'parent_article_id': 1,
...         'posted': ISODate("2017-03-22T19:53:59.526+0000"),
...         'author': 'Alice',
...         'comment_body': 'Test comment' })
WriteResult({ "nInserted" : 1 })

We can view this comment (and all contents of the database) with the command:

db.comments.find()

This will return the only comment we have inserted so far:

{ "_id" : ObjectId("58d2df9d8da74c010860e576"), "parent_article_id" : 1, "posted" : ISODate("2017-03-22T19:53:59.526Z"), "author" : "Alice", "comment_body" : "Test comment" }

Add a second comment for good measure:

db.comments.insert({
    'parent_article_id': "1",
    'posted': ISODate("2017-03-22T22:11:08.526+0000"),
    'author': `Bob`,
    'comment_body': 'Hello everyone.' })

To display all comments for a particular article:

db.comments.find({ 'parent_article_id': "1" })