Best Document Format for Address Book in CouchDB

I have really tried, tried so hard, but I can't figure out couchdb :( I have to record the contact of multiple people, should I put each contact in one document?

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

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

Future records:

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

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

      

Or in different documents?

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

 "number" : "Jake number"
}

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

 "number" : "Lock number"
}

Future records:

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

 "number" : "Jake number"
}

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

 "number" : "Lock number"
}

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

 "number" : "Kate number"
}

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

 "number" : "Jin number"
}

...

      

I am confused, can anyone help me?

Thanks.

+2


a source to share


1 answer


I am assuming that you store these contacts to form some kind of address book style application. Based on this assumption, I would say that your second example is exactly what you want to do. The way I look at it, each "contact" is one document. All attributes of this contact belong to the document.

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

      

To take this a little further, in the future you may decide that you want to store multiple numbers per person, possibly of different types. I would nest all of this inside a document for a specific 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 get closer to designing a model for use in a document database to consider which elements you want to use yourself. For those that make sense on their own, they should probably go into their own document. For those that only make sense when viewed in the context of their "container" object, embed them in it.

Hope this helps you.

+3


a source







All Articles