开发者

mongodb best practice: nesting

Is this example of nesting generally accepted as good or bad practice (and why)?

A collection called users:

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: From the answers in this post I've updated the schema applying the following rules (the data is slightly different from above):

  • Nest, but only one level deep
  • Remove unneccesary ke开发者_如何转开发ys
  • Make use of arrays to make 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!


In my opinion above schema not 'generally accepted', but looks like great. But i suggest some improvements thats will help you to query on your document in future:

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

Nesting is always good, but two or three level nesting deep can create additional troubles in quering/updating.

Hope my suggestions will help you make right choice of schema design.


You may want to take a look at schema design in MongoDB, and specifically the advice on embedding vs. references.

Embedding is preferred as "Data is then colocated on disk; client-server turnarounds to the database are eliminated". If the parent object is in RAM, then access to the nested objects will always be fast.


In my experience, I've never found any "best practices" for what a MongoDB record actually looks like. The question to really answer is, "Does this MongoDB schema allow me to do what I need to do?"

For example, if you had a list of addresses and needed to update one of them, it'd be a pain since you'd need to iterate through all of them or know which position a particular address was located. You're safe from that since there is a key-value for each address.

However, I'd say nix the basic and contact keys. What do these really give you? If you index name, it'd be basic.name rather than just name. AFAIK, there are some performance impacts to long vs. short key names.

Keep it simple enough to do what you need to do. Try something out and iterate on it...you won't get it right the first time, but the nice thing about mongo is that it's relatively easy to rework your schema as you go.


That is acceptable practice. There are some problems with nesting an array inside of an array. See SERVER-831 for one example. However, you don't seem to be using arrays in your collection at all.

Conversely, if you were to break this up into multiple collections, you would have to deal with a lack of transactions and the resulting race conditions in your data access code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜