What is the purpose of embeds_one in Mongoid?

I understand the concept of using embeds_many , but what purpose does embeds_one especially when you have only one field in an inline document, as shown in the example with a 1-N inline document on mongoid.org? Wouldn't it be better to create a field in the parent document?

Example at mongoid.org:

 class Band include Mongoid::Document embeds_one :label end class Label include Mongoid::Document field :name, type: String embedded_in :band end 
+4
source share
1 answer

In the example, they gave one example field only to display the structure of the definition. You can enter several fields in embedded documents.

embeds_one is used when there are several fields in the embedded document that you do not want to store in the main document. When some fields are not mandatory for all documents that we do not enter in the main document, because this will make the document heavy. Instead, we introduce an embedded document that you can create based on the requirements of the main document.

0
source

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


All Articles