Tutorial: Use MongoDB to Store Comments

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.

Requirements

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

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.

Free Cloud Server Trial from IONOS

Try out a Cloud Server for free now - test your IONOS Cloud Server for 30 days!

REST API
Unlimited traffic
VMware virtualization

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" })
Tip: Managed MongoDB from IONOS

Managed MongoDB from IONOS enables you to concentrate on the essentials. From installation to operation and maintenance work, IONOS makes sure you always get the best performance from your data banks.

We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.