开发者

Best document format for addressbook in CouchDB

I really tried, tried so hard but i cant understand couchdb :( I must record the contact of several people, should i put every contact in a single document ?

"1th documet"
{
 "names" : [
  Jake", "Lock"
 ]

 "numbers" : [
  "Jake's number", "Lock's number"
 ]
}

Future records:

"1th documet"
{
 "names" : [
  Jake", "Lock", "Kate", "Jin", ...
 ]

 "numbers" : [
  "Jake's number", "Lock's number", "Kate's number", "Jin's number", ...
 ]
}

Or in different documents ?

"1th document"
{
 "name" : "Jake"

 "number" : "Jake's number"
}

"2th document"
{
 "name" : "Lock"

 "number" : "Lock's number"
}

Future records:

"1th document"
{
 "name" : "Jake"

 "number" : "J开发者_开发技巧ake's number"
}

"2th document"
{
 "name" : "Lock"

 "number" : "Lock's number"
}

"3th document"
{
 "name" : "Kate"

 "number" : "Kate's number"
}

"4th document"
{
 "name" : "Jin"

 "number" : "Jin's number"
}

...

I confused, can somebody help me ?

Thanks.


I assume you are storing these contacts to form some kind of address-book style application. Going with this assumption, I would say your second example is exactly what you want to be doing. The way I look at it, each "contact" is a single document. All the attributes for this contact belong within the document.

{
    name: "John Smith",
    number: "+44 1234 567890"
}

To take this a bit further, in the future you might decide you wish to store multiple numbers per person, perhaps of different types. I would embed these all inside the document for the particular contact:

{
    name: "John Smith",
    numbers: [
        { number: "+44 1234 567890", type: "home" },
        { number: "+44 7798 987654", type: "mobile" },
        { number: "+44 1234 987123", type: "work" }
    ]
}

I find a good way to approach designing a model for use in a document database is to consider what items you will wish to use independently. For those which make sense on their own, they should probably go inside their own document. For those which only make sense when viewed in the context of their "container" object, embed them within it.

I hope this helps you.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜