Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Traditional table based relation databases have solved this problem. Each record/document that could be referenced in multiple contexts is stored in it own table/collection with an <code>id</code>. That <code>id</code> is referenced by other objects in their own collections.</p> <p><code>JSON</code> itself will only hold raw data, so as you note, object references that change is not something <code>JSON</code> can handle.</p> <p>So the question becomes, is the data a relationship between first order objects? Or is it nested/embedded in another object?</p> <p>In your example, a project has many people, a project has many cats, and people has many projects. So because people and projects both need to be referenced by multiple other objects, each resource should be it's own resource, reference by a foreign key.</p> <pre><code>// projects.json [ { id: 4 name: 'homework', person_ids: [1,2,3], } ] // people.json [ { id: 1, name: 'Bob', project_ids: [4,5,6], } ] </code></pre> <p>A good case for embedding might be comments on a project. In this case, you always access the comments through the projects. And because a comment can only ever belong to a single project, embedding it makes more sense.</p> <pre><code>// projects.json [ { id: 4 name: 'homework', person_ids: [1,2,3], comments: [ { person_id: 1, message: 'My dog ate it' ] ] } ] </code></pre> <hr> <p>I'd recommend reading up on how <a href="http://mongoid.org/en/mongoid/docs/relations.html#has_many" rel="nofollow">Mongoid handles relations</a>. It's a ruby wrapper for MongoDB, but the docs explain how it structures the collections to enable relationships. You might find it quite helpful to get your head around how this would work.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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