How does mongoDB work?

Possible duplicate:
How to present data for streaming comments (along with voting comments) in mongodb?

I came from the mysql world and decided to learn how to use mongoDB. My upcoming project will be similar to Reddit.com with multi-level commenting and voting. Can you direct me to a good resource to understand how to create applications using mongodb? I understand how the basic document repository works, but I can’t wrap myself around how I actually store the comment tree and user information in one document and update it if someone updates the user information. So basically I'm looking for a guide on migrating from mysql to mongodb.

Any advice / recommendations are appreciated.

+7
source share
2 answers

Some rules of thumb that I found useful are as follows:

  • If there is only one logical copy of part of the information, it should be in one document (for example, if you have comments on the message, the easiest way is to include them in the message)

  • If you denormalize the data on SQL ground in some other table to avoid joining and something else, then the same behavior applies to the document repository: denormalize from one "main" place in the copy to other places. Copies should be considered as copies, not source information, so they can be overwritten by future denormalization actions.

  • If you need to access a canonical dataset, such as a user account, from multiple locations, store the links as ObjectId in mongodb, then run the second query for the linked document. You should know in your application that the second request is not a union, and will not block both documents to ensure consistency, therefore there may be inconsistencies in the results.

Essentially, you should think that your database is consistent at the document level. Any request for related documents can be inconsistent, so if you need consistency, you can denormalize this data into one document.

If you need a user account that will exactly match your comments, you will have to copy the relevant information next to your comments while writing the comments in the document. This means that you must constantly think about application-level consistency. If not, then, as I suspect, this is just a question for the user.

If you are concerned about performance when requesting data for all users participating in your page, I would recommend copying some data from the user account next to the comment, but only reading from this copy - you should write to your original user accounts.

This is all that comes to mind now, but I can edit, since everything happens to me :)

+8
source

The reason you are having problems is enveloping you because MongoDB is not a relational database , it is a document-oriented database . For something simple, such as a comment tree with a very complex structure and a one-to-many relationship, you are probably best offended by MySQL. A user profile may be interesting to use MongoDB, but then again, if it is very structured, you might be better off with MySQL.

You might want to determine which aspects of the project are best suited for a documented database (IE: unstructured data), and which aspects are best used with a more traditional relational database, and then use both!

The previous question I asked has a good overview of both: Are document-oriented databases replacing relational databases?

I have also used both projects with great success, although this requires a significant part of the initial setup, since most frameworks do not allow them to be implemented too easily.

+5
source

All Articles