Relations in Document-oriented database?

前端 未结 6 1426
北恋
北恋 2021-02-18 23:14

I\'m interested in document-oriented databases, and I\'d like to play with MongoDB. So I started a fairly simple project (an issue tracker), but am having hard times thinking in

6条回答
  •  暖寄归人
    2021-02-19 00:06

    In my mind this is actually pretty simple. Embedded documents can only be accessed via their master document. If you can envision a need to query an object outside the context of the master document, then don't embed it. Use a ref.

    For your example

    issue = {code:"asdf-11", title:"asdf", reporter:{username:"qwer", role:"manager"}}
    

    I would make issue and reporter each their own document, and reference the reporter in the issue. You could also reference a list of issues in reporter. This way you won't duplicate reporters in issues, you can query them each separately, you can query reporter by issue, and you can query issues by reporter. If you embed reporter in issue, you can only query the one way, reporter by issue.

    If you embed documents, you can update them all in a single query, but you have to repeat the update in each master document. This is another good reason to use reference documents.

提交回复
热议问题