Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store comments in NoSQL (MongoDB)?

Tags:

mongodb

nosql

1 way

Comments are embedded in Post document:

{
  "_id": ObjectId(12345),
  "title": "Cat ate elephant!",
  "body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean convallis pretium vulputate.",
  "comments": [
    {"name": "Cat", "body": "MEOW!"},
    {"name": "Elephant", "body": "I was eaten by cat, lol!"},
    {"name": "Human", "body": "I am hungry!"}
  ]
}

2 way

Relationship between Post and Comments (in separate documents). Post has many Comments:

// POST //
{
  "_id": ObjectId(12345),
  "title": "Cat ate elephant!"
  "body": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean convallis pretium vulputate."
}


// Comments //

{
  "_id": ObjectId(...),
  "post_id": ObjectId(12345),
  "name": "Cat",
  "body": "MEOW!"
}

{
  "_id": ObjectId(...),
  "post_id": ObjectId(12345),
  "name": "Elephant",
  "body": "I was eaten by cat, lol!"
}

{
  "_id": ObjectId(...),
  "post_id": ObjectId(12345),
  "name": "Human",
  "body": "I am hungry!"
}

Which way is better?

like image 654
rgtk Avatar asked Apr 14 '12 13:04

rgtk


People also ask

How do I comment in MongoDB?

The $comment operator can be used to add a comment to any query predicate in MongoDB to understand the purpose of that query. As its name directs, it acts as a comment in MongoDB just like anyone adds a comment in any other programming language.

How does MongoDB store data in database?

To save the data into the database, we need to create a new instance of our model that we created early. We will pass into this instance the user's input. Once we have it then we just need to enter the command "save". Mongoose will return a promise on a save to the database.

Can we store data in MongoDB?

Large objects, or "files", are easily stored in MongoDB. It is no problem to store 100MB videos in the database. This has a number of advantages over files stored in a file system. Unlike a file system, the database will have no problem dealing with millions of objects.

Which is better NoSQL or MongoDB?

Difference Between MongoDB and NoSQLA MongoDB “database” is the top-level container, consists of one or more collections while NoSQL data stores provide a top-level namespace or container for storing data. MongoDB is based on the document store data model in which a document is stored as BSON format.


2 Answers

Method 1

  • No need to joins => fast access to data
  • The NoSQL way of doing it
  • Every comment is just related to just that post, so why not store them together(you store the title and the body together ;)

If you have large documents, >15.5 Megabyte, and you gets a loot of comments then you probably will need to store them somewhere else. This because the maximum document size is 16 Megabyte.

Method 2 is the RDMBS way of doing it, Mongo does not have joins inbuilt so you will need to do them in your application.

like image 53
Mattias Avatar answered Oct 07 '22 12:10

Mattias


The first way is preferred, as long as the document is not write-heavy. If you're going to have 5000 comments added to a post within a minute, then use the second method.

like image 3
Joe Frambach Avatar answered Oct 07 '22 11:10

Joe Frambach