Uploaded image for project: 'Python Integrations'
  1. Python Integrations
  2. INTPYTHON-358

Support Embedded Field and Array Creation

    • Type: Icon: Task Task
    • Resolution: Unresolved
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: None
    • Component/s: django
    • None
    • Python Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      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.

            Assignee:
            jib.adegunloye@mongodb.com Jib Adegunloye
            Reporter:
            jib.adegunloye@mongodb.com Jib Adegunloye
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: