How to structure a database design for a simple photo album in couchdb?
I want to try CouchDb by developping a little PhotoAlbum application where different users can have many albums with many photos in them. Am i doing it right if I create a document for each users, that contains an array of albums, that contains photos with attachments?
{
user: "Dominik",
albums: [
{ name: "USA Trip", photos:
开发者_开发技巧 [
{ title: "Golden Gate Bridge", _attachments: [ the photo ] },
{ another photo }
]
},
{ ...} ]
}
or is there another good way to do this?
or is it better to store users, albums and photos each in different documents? This would require foreign keys as in mysql?
{ type: "user", name: "Dominik", albums: [ "a1", "a2", "a3" ] }
{ type: "album", _id: "a1", title: "USA Trip", photos: [ "p1", "p2" ... ] }
{ type: "photo", _id: "p1", _attachments: {...}, title: "Golden Gate Bridge" }
...
or the other way around?:
{ type: "user", _id: "u1", name: "Dominik" }
{ type: "album", _id: "a1", title: "USA Trip", user: "u1" }
{ type: "photo", _id: "p1", _attachments: {...}, title: "Golden Gate Bridge", album: "a1" }
1999 is right. Two more minor points:
- Remember, Apache CouchDB supports atomic transactions—but only within a document. You can change anything in a document with a single atomic operation.
- In the CouchDB security framework, your security policy is applied one document at at time, in isolation. The
validate_doc_update
function cannot see foreign documents when it has to decide what a user may do. (If you do not use CouchDB for accounts and authentication, this is not relevant.)
From my point of view it's better to divide your data into small portions of it. But this is only your choice. i chose dividing because i was faced with memcached-server limit of 1Mb per "key-value-instance" (we cache CouchDB documents in memcached), so we couldn't store our data in one document.
But of course there are advantages of storing your data in one document. I hope other people will tell about their experience and you will choose what fits you best.
精彩评论