質問

I really like the document-based approach of storing data like blog posts as a whole document with all information needed saved inside of it. Therefore the author´s username is stored as plain text. The author himself has his own document with personal information attached to it. What happens when the author decides to change his username? Do I have to update every document the contains a blog post by that author or is this just one of the drawbacks using a document-based database?

Thanks for any suggestions!

役に立ちましたか?

解決

If you need to write a query(view) with content from the blogpost and the name of the author, then the name must be included in the blog content, and therefore all blogposts must be updated.

if the name is only for information ( i mean you do not query a blogpost-content like keywords AND name of author), you can add the id into the blog document (and of course now can Query blog content AND author-id) and emit {'_id':doc.author_id} as a Value. include_docs=true then gives you the doc of the Author (and no longer the blogpost-doc.. you have to call it explicit with the id thats in the result rows). No Need to update the blogposts.

Example: Case 1: Use Author by Name, you have to include the name, and therefore update ALL docs.

{
 "_id":"blogpost1",
 "author":"Oliver",
 "keyword":"couchDB"
}

to look for all couchdb posts from oliver:

emit ([doc.author,doc.keyword],1) 

call:

&key=["Oliver","couchDB"]

Case 2: No need to query by name

    {
     "_id":"blogpost1",
     "author_id":"author-123",
     "keyword":"couchDB"
    }

   emit (doc.keyword,{'_id':doc.author_id})

and the authors doc:

{
"_id":"author-123",
"name":"Oliver"
}

call:

?key=["couchDB"]&include_docs=true

result:

...
{"id":"blogpost1","key":"couchDB","value":{"_id":"author-123"},"doc":{"_id":"author-123","_rev":"xxx","name":"Oliver,....
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top