The answer to this really depends on what intend to do with the data you are storing in mongodb. It is important to remember that a ReferenceField
will point to a document in another collection in mongodb, whereas an EmbeddedDocument
is stored in the same document in the same collection.
Consider this schema:
Person
-> name
-> address
Address
-> street
-> city
-> country
If you expect every person to have only one address and each address to only be associated with one person (a one-to-one relationship) and you are generally going to query the database for one or more Person
documents then the Person.address field should be EmbeddedDocumentField
.
If you expect every person to have more than one address but each address will only be associated to one person (a one-to-many relationship) and you will still mainly query for a Person then you can use an EmbeddedDocumentListField
.
If you expect every person to have more than one address and each address will be associated with many people (a many-to-many relationship) you probably should use ReferenceField
.
However, even if you are one-to-one or one-to-many, if the Address
is part of your data model that is of interest then it may be advantageous to have it stored in it's own collection because it makes aggregation and indexing easier.
One other point to consider is that unless you turn it off mongoengine will de-reference every ReferenceField
when you retrieve a document - this might introduce performance penalties with lots of ReferenceField
or references to very large documents.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…