Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Even MongoDB doesn't support joins, you can organize data in several different ways:</p> <p>1) First of all, you can <strong>inline</strong>(or embed) related documents. This case is useful, if you have some hierarchy of document, e.g. post and comments. In this case you can like so:</p> <pre><code>{ _id: &lt;post_id&gt;, title: 'asdf', text: 'asdf asdf', comments: [ {&lt;comment #1&gt;}, {&lt;comment #2&gt;}, ... ] } </code></pre> <p>In this case, all related data will be in the save document. You can fetch it by one query, but pushing new comments to post cause moving this document on disk, frequent updates will increase disk load and space usage. </p> <p>2) <strong>referencing</strong> is other technique you can use: in each document, you can put special field that contains <strong>_id</strong> of parent/related object:</p> <pre><code>{ _id: 1, type: 'post', title: 'asdf', text: 'asdf asdf' }, { _id:2 type: 'comment', text: 'yep!', parent_id: 1 } </code></pre> <p>In this case you store posts and comments in same collection, therefor you have to store additional field <strong>type</strong>. MongoDB doesn't support constraints or any other way to check data constancy. This means that if you delete post with _id=1, comments with _id=2 store broken link in <strong>parent_id</strong>.</p> <p>You can separate posts from comments in different collections or even databases by using <a href="http://docs.mongodb.org/manual/reference/database-references/" rel="nofollow">database references</a>, see your driver documentation for more details.</p> <p>Both solutions can store tree-structured date, but in different way.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload