MongoDB is a popular NoSQL database which is perfectly suited for a task like storing comments on a high-traffic site where avail­abil­i­ty 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.

Re­quire­ments

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

Website comments are an excellent use case for MongoDB versus a tra­di­tion­al re­la­tion­al database. The user-entered data (like the username) is erratic, which can cause duplicate record headaches with a re­la­tion­al database. Comments may be submitted quickly and in great volume during high-traffic times, which requires high per­for­mance.

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

Free Cloud Server Trial
En­ter­prise-grade virtual private servers
  • KVM based dev servers for de­vel­op­ers
  • Scalable to en­ter­prise cloud level
  • Pay-as-you-go, per-minute billing

Project Overview

There are many possible ap­proach­es for this project. In par­tic­u­lar, 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 sit­u­a­tions where a document will grow in size over time. Ad­di­tion­al­ly, the larger a document gets, the more difficult it is to store on the disk. This can cause sig­nif­i­cant read/write per­for­mance 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 au­then­ti­cat­ed).
  • 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 par­tic­u­lar article:

db.comments.find({ 'parent_article_id': "1" })
Tip: Managed MongoDB from IONOS

Managed MongoDB from IONOS enables you to con­cen­trate on the es­sen­tials. From in­stal­la­tion to operation and main­te­nance work, IONOS makes sure you always get the best per­for­mance from your data banks.

Go to Main Menu