Mongodb real basic use case

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!


  • NoSQL databases are used for storage of non-sensitive data for instance posts, comments..
  • 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.


    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.

    链接地址: http://www.djcxy.com/p/78048.html

    上一篇: 在我的Django模板中获取绝对静态文件的URL

    下一篇: Mongodb真正的基本用例