mongodb best practice: nesting

后端 未结 4 746
盖世英雄少女心
盖世英雄少女心 2020-12-30 08:06

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

A collection called users:

user
    basic
        name : value
              


        
4条回答
  •  无人及你
    2020-12-30 08:19

    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.

提交回复
热议问题