简体   繁体   中英

how should I handle shared images in mongodb?

Lets say I have a Posts document with an embedded author.

{
    name: "some post",
    text: "some text",
    author:{
        avatar_img: "http://www.someimage.com/1"
    },
    comments: [
        {
            user: "some name"
            avatar_img: "http://www.someimage.com/5"
        }
    ]
}

Now lets say I consume this data in the frontend and display the post with the author's image on the page. What happens when the author changes his avatar url? I would have to update every post with the new image url. Is this the right way to do it? The only option I see is to reference a "Users" document, but then every post will have 2 db requests. Not to mention all the comments will also require 2 db requests to get the image url as well....

There are two approaches on the system design level - when someone changes their avatar should you make their previous made posts display the old or the new avatar?

Some might claim that if you had avatar [X] when you posted, then going back to that post should display the same avatar image "forever". Others might think that avatar image for author is their identifier and it should be changed to match their current avatar [Y].

If you want to do the former, you're already done :)

If you want to do the latter, then when an author changes their avatar from [X] to [Y] then you could run a single update which would be something like:

db.posts.update({ "author.name":"joejoe", "author.avatar_img":"[X]"}, 
                { $set:{"author.avatar_img":"[Y]"}},
                { multi : true }
               );

I've made some assumptions here - one is that you store the author's name and not just the avatar_img, the other is that each post can only have one author, third is that your multi-update could get interrupted which is why my matching condition is both author name and their old avatar.

That takes care of author. Now, what about comments? I'm going to tell you that embedding comments into the post document is a "Bad Idea(tm)" - you want to limit the continuous growth of documents for performance reasons so at most you might embed the first few comments into the document itself (so that you can display the first few comments when you display the post) but the comments belong in their own collection where you can query for them if the viewer asks to see more comments. Once they are in a separate collection you can do the same update against it as you did against posts (if you want to). Since you will limit the number of array elements in post to a handful of comments you can then easily update those using another one (or a few) update commands.

Note that updating the avatar in denormalized form will be asynchronous when a user changes their avatar but that's okay because users don't change avatars a lot, you don't care if the update takes a little longer but you will gain huge benefit in performance by optimizing the most frequent operation - displaying a post to a view with only one read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM