Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb real basic use case

Tags:

mongodb

nosql

I'm approaching the noSQL world. I studied a little bit around the web (not the best way to study!) and I read the Mongodb documentation. Around the web I wasn't able to find a real case example (only fancy flights on big architectures not well explained or too basic to be real world examples).

So I have still some huge holes in my understanding of a noSQL and Mongodb.

I try to summarise one of them, the worst one actually, here below:

Let's imagine the data structure for a post of a simple blog structure:

{
"_id": ObjectId(),
"title": "Title here",
"body": "text of the post here",
"date": ISODate("2010-09-24"),
"author": "author_of_the_post_name",
"comments": [
              {
              "author": "comment_author_name",
              "text": "comment text",
              "date": ISODate("date")
              },
              {
              "author": "comment_author_name2",
              "text": "comment text",
              "date": ISODate("date")
              },
              ...
            ]
}

So far so good.

All works fine if the author_of_the_post does not change his name (not considering profile picture and description). The same for all comment_authors.

So if I want to consider this situation I have to use relationships:

"authorID": <author_of_the_post_id>,

for post's author and

"authorID": <comment_author_id>,

for comments authors.

But MongoDB does not allow joins when querying. So there will be a different query for each authorID.

So what happens if I have 100 comments on my blog post?

1 query for the post
1 query to retrieve authors informations
100 queries to retrieve comments' authors informations

**total of 102 queries!!!**

Am I right?

Where is the advantage of using a noSQL here? In my understanding 102 queries VS 1 bigger query using joins.

Or am I missing something and there is a different way to model this situation?

Thanks for your contribution!

like image 475
user3096364 Avatar asked Mar 08 '14 17:03

user3096364


People also ask

Where do we use MongoDB in real life?

Product Data Management For e-commerce websites and product data management and solutions, one can use MongoDB to store information because it has the flexible schema well suited for the job. One can also manage a product catalog and learn the practices and methods for modeling from the Product Catalog document.

What is MongoDB best used for?

MongoDB has become one of the most wanted databases in the world because it makes it easy for developers to store, manage, and retrieve data when creating applications with most programming languages.

Is MongoDB worth learning 2022?

This is where MongoDB is useful. It is based on the NoSQL document store model. MongoDB has become quite popular in the last few years with the number of users growing at almost the same rate as PostgreSQL. For a very different kind of database application, of course.

Which applications uses MongoDB?

Over the years, MongoDB has become a popular choice of a highly scalable database and it is currently being used as the backend data store of many well-known organizations like IBM, Twitter, Zendesk, Forbes, Facebook, Google, and a gazillion others.


2 Answers

Have you seen this?

http://www.sarahmei.com/blog/2013/11/11/why-you-should-never-use-mongodb/

It sounds like what you are doing is NOT a good use case for NoSQL. Use relational database for basic data storage to back applications, use NoSQL for caching and the like.

like image 129
fregas Avatar answered Sep 30 '22 13:09

fregas


  1. NoSQL databases are used for storage of non-sensitive data for instance posts, comments..
  2. You are able to retrieve all data with one query. Example: Don't care about outdated fields as author_name, profile_picture_url or whatever because it's just a post and in the future this post will not be visible as newer ones. But if you want to have updated fields you have two options:
    • First option is to use some kind of worker service. If some user change his username or profile picture you will give some kind of signal to your service to traverse all posts and comments and update all fields his new username.
    • Second option use authorId instead of author name, and instead of 2 query you will make N+2 queries to query for comment_author_profile. But use pagination, instead of querying for 100 comments take 10 and show "load more" button/link, so you will make 12 queries.

Hope this helps.

like image 32
Nikola Mitev Avatar answered Sep 30 '22 12:09

Nikola Mitev