Is illustration of nesting generally recognized as bad or good practice (and why)?

An assortment known as customers:

user
    basic
        name : value
        url : value
    contact
        email
            primary : value
            secondary : value
        address
            en-gb
                address : value
                city : value
                state : value
                postalcode : value
                country : value
            es
                address : value
                city : value
                state : value
                postalcode : value
                country : value

Edit: In the solutions within this publish I have up-to-date the schema using the next rules (the information is slightly not the same as above):

  • Nest, only one level deep
  • Remove unneccesary secrets
  • Take advantage of arrays to create objects more flexible

    {
       "_id": ObjectId("4d67965255541fa164000001"),
       "name": {
         "0": {
           "name": "Joe Bloggs",
           "il8n": "en" 
          } 
        },
       "type": "musician",
       "url": {
         "0": {
           "name": "joebloggs",
           "il8n": "en" 
          } 
        },
       "tags": {
         "0": {
           "name": "guitar",
           "points": 3,
           "il8n": "en" 
          } 
        },
       "email": {
         "0": {
           "address": "joe.bloggs@example.com",
           "name": "default",
           "primary": 1,
           "il8n": "en" 
          } 
        },
       "updates": {
         "0": {
           "type": "news",
           "il8n": "en" 
          } 
        },
       "address": {
         "0": {
           "address": "1 Some street",
           "city": "Somecity",
           "state": "Somestate",
           "postalcode": "SOM STR",
           "country": "UK",
           "lat": 49.4257641,
           "lng": -0.0698241,
           "primary": 1,
           "il8n": "en" 
          } 
        },
       "phone": {
         "0": {
           "number": "+44 (0)123 4567 890",
           "name": "Home",
           "primary": 1,
           "il8n": "en" 
          },
         "1": {
           "number": "+44 (0)098 7654 321",
           "name": "Mobile",
           "il8n": "en" 
          } 
        } 
    }
    

Thanks!

For me above schema not 'generally accepted', but appears like great. However i suggest some enhancements thats will help you query in your document later on:

User
    Name 
    Url
    Emails {email, emailType(primary, secondary)}
    Addresses{address, city, state, postalcode, country, language}

Nesting is definitely good, but 2 or 3 level nesting deep can make additional troubles in quering/upgrading.

Hope my suggestions can help you make right selection of schema design.

In my opinion, I have never found any "guidelinesInch for which a MongoDB record really appears like. The question to actually response is, "Performs this MongoDB schema let me do what I have to do?"

For instance, should you have had a listing of addresses and required to update one of these, it would be a discomfort since you'd have to iterate through these or know which position a specific address was situated. You are protected from that since there's a vital-value for every address.

However, I'd say nix the basic and contact secrets. Exactly what do these really provide you with? Should you index name, it would be basic.name as opposed to just name. AFAIK, you will find some performance impacts to lengthy versus. short key names.

Make it simple enough to complete what you ought to do. Try something out and iterate onto it...you will not understand it properly the very first time, however the nice factor about mongo is the fact that it's relatively simple to change your schema along the way.

You might want to have a look at schema design in MongoDB, and particularly the recommendation on embedding vs. references.

Embedding is preferred as "Information is then colocated on disk client-server turnarounds towards the database are removed". When the parent object is within RAM, then accessibility nested objects will be fast.

That's acceptable practice. You will find some issues with nesting an assortment within an assortment. See SERVER-831 for just one example. However, you do not appear to become using arrays inside your collection whatsoever.

On the other hand, should you break this up into multiple collections, you would need to cope with deficiencies in transactions and also the resulting race conditions inside your data access code.