-
Type: Task
-
Resolution: Unresolved
-
Priority: Critical - P2
-
None
-
Affects Version/s: None
-
Component/s: django
-
None
Context
In Django, all relationships are by reference by default (minus JSONFields). In MongoDB, schema design best practice takes advantage of denormalization via subdocuments and arrays.
Let's use the example in: https://www.mongodb.com/docs/manual/tutorial/model-embedded-one-to-one-relationships-between-documents/
The example schema contains two entities, a user and an address. In Django/SQL these would be represented in two separate tables:
# user document { "_id": "joe", "name": "Joe Bookreader" } # address document { "user_id": "joe", "street": "123 Fake Street", "city": "Faketon", "state": "MA", "zip": "12345" }
The address data is frequently retrieved with the user information. To allow your application to retreive all necessary information with a single query, embed the address information inside of the user document:
{ "_id": "joe", "name": "Joe Bookreader", "address": { "street": "123 Fake Street", "city": "Faketon", "state": "MA", "zip": "12345" } }
This is the Embedded Document Pattern.
Now let's look at embedded arrays: https://www.mongodb.com/docs/manual/tutorial/model-embedded-one-to-many-relationships-between-documents/
Let's use the same example as above but change the schema such that a user can have multiple addresses. In Django/SQL these would again be represented in two separate tables:
# address document one { "user_id": "joe", "street": "123 Fake Street", "city": "Faketon", "state": "MA", "zip": "12345" } # address document two { "user_id": "joe", "street": "1 Some Other Street", "city": "Boston", "state": "MA", "zip": "12346" }
In MongoDB, this would often be modelled with an embedded array, like this:
{ "_id": "joe", "name": "Joe Bookreader", "addresses": [ { "street": "123 Fake Street", "city": "Faketon", "state": "MA", "zip": "12345" }, { "street": "1 Some Other Street", "city": "Boston", "state": "MA", "zip": "12346" } ] }
Another even simpler example which takes advantage of an embedded is when the user wants to store a list of scalar values:
{
"values": [1, 2, 3, 4, 5, 6]
}
Definition of done
We should be able to model embedded documents and arrays in Django-MongoDB otherwise we will need to perform excessive joins which leads to poor performance.
Design ideas
Introduce new Field and Model classes to represent embedded Document and embedded array fields.
Pitfalls
TBD.