Questions about MongoDB Database Design

  node.js, question

Question 1: Is it better to have as few collection as possible and try to nest the relationships represented by tables in the relational database into the document inside?
Question 2: In this case, mongodb may have to query many times for a complex query that SQL can handle. Is mongodb still faster than sql database?
Question 3: What are mongodb’s advantages? Mapreduce for Very Large Scale Data? Easy to expand?

I comeTake a chestnutRight:

Suppose this is what mysql says:

authors (
 int id,
 char name,
 int age,
 char email
 )
 
 articles (
 int id,
 char title,
 char content,
 long viewCount,
 int author_id
 )

Then it might look like this in MongoDB:

  • There is only one authoriscollection.
author {
 _id: new ObectID("blublublu"),
 name: 'portwatcher',
 age: '19',
 email: 'root@pwhack.me',
 articles: [{
 title: 'you guess',
 content: 'I am content',
 viewCount: 52345
 }, ...]
 bracket

The question is, if I want to find out all the authors’ articles separately and sort them by the number of views, what should I do?

  • Therefore, there is a second design method, which is also the embodiment of nosql = not only sql. There are two collection of authors and articles.
author {
 _id: new ObectID("blublublu"),
 name: 'portwatcher',
 age: '19',
 email: 'root@pwhack.me'
 bracket
 
 article {
 _id: new ObjectID("lalalala"),
 title: 'you guess',
 content: 'I am content',
 viewCount: 52345,
 author_id: 'blublublu'
 bracket

The question now is, what if I want to return the article with the author’s name?
1. Do you want to check twice, even twice? If it is repeated, some paas are not supported (for example, bae is not supported by pro-test). Is this not elegant?
2. If an author’s name is stored in article inside, when an author changes his name, the author’s name displayed in the article cannot be updated. Is it too expensive to update it together?
3. when is dbref suitable? How to use it here?

HereChestnutTo sum up what we need:

  • All articles owned by all authors can be aggregated and returned and sorted in some way.
  • The article can be returned with the matching author name.
  • The author can edit his own information.
  • Both the article and the author can be inserted separately.

It may be a bit wordy and understandable.

It would be better if someone could summarize some principles of mongodb database design ~

Mongodb is not an rdbms. You are free to initiate any select type query. such as

for ($i=0,$j<100000000;  $i<$j;  $i plus)
 $author = $mongo->getCollection('db','table')->findOne(array('author_id'=>id),array('author_name'=>1));
 echo $author['author_name'];

Mysql, you are crazy. Mongodb, this is good.

You should think fundamentally that mongodb is absolutely different from MySQL.

Of course, there is no denying that the fewer queries, the better-#