MongoDB embedded documents and links to unique ObjectIds for the system user profile

I would like to code a web application in which most sections depend on the user profile (for example, different to-do lists per person, etc.), and I would like to use MongoDB. I was thinking of creating about 10 embedded documents for the main profile document and saving everything that is connected with one user inside his own document.

I don’t see a clear way to use foreign keys for mongodb, the only way would be to create a to_do_id field with the ObjectId type, for example, but they would not be completely interconnected, it would just happen to have the same Identifiers, which I would have to request.

  • Is there a limit on the number of built-in document types within a top-level document, which can degrade performance?
  • How do you solve the problem with a central profile document that most documents have to deal with when presenting a presentation to a person?
  • Do you use semi-interval keys inside MongoDb and have fields with ObjectId types that will have some other unique identifier for the document, and not embed them?

I can’t understand what approach should be taken when. Thank you very much!

+6
source share
1 answer
  • There is no specific limitation regarding performance. However, the larger the document, the more time is required for transmission by cable. The entire document is always checked out.
  • I am doing this with links. You can choose between simple reference links and the DBRef database according to this page: http://www.mongodb.org/display/DOCS/Database+References
  • The link above the document, how to have links in the document according to the semi-foreign key method. A DBRef may be good for what you are trying to do, but a simple manual link is very effective.

I am not sure that there is a general rule for which it is best to use a referential approach. Since I mainly use Java or Groovy, I like the fact that I get a returned DBRef object. I can check this data type and use it to decide how to handle the link in a general way.

Therefore, I usually use a simple reference guide for links to different documents in one collection and DBRef for links between collections.

I hope this helps.

+2
source

Source: https://habr.com/ru/post/897010/


All Articles