简体   繁体   English

我应该如何在mongodb中处理共享图像?

[英]how should I handle shared images in mongodb?

Lets say I have a Posts document with an embedded author. 可以说我有一个带有嵌入式作者的Posts文档。

{
    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? 作者更改头像URL会怎样? I would have to update every post with the new image url. 我将不得不用新的图像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. 我看到的唯一选择是引用“用户”文档,但是每个帖子都会有2个数据库请求。 Not to mention all the comments will also require 2 db requests to get the image url as well.... 更不用说所有注释也将需要2个db请求才能获取图像URL。

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". 有人可能会说,如果您发布时具有化身[X],那么回到该帖子应显示相同的化身图像“永远”。 Others might think that avatar image for author is their identifier and it should be changed to match their current avatar [Y]. 其他人可能认为作者的头像图片是他们的标识符,应该对其进行更改以匹配其当前头像[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: 如果要进行后者,那么当作者将其头像从[X]更改为[Y]时,您可以运行一个更新,如下所示:

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. 我在这里做了一些假设-一种是您存储作者的姓名,而不仅仅是avatar_img,另一种是每个帖子只能有一位作者,第三种是您的多次更新可能会中断,这就是为什么我的匹配条件既是作者名, 又是他们的旧头像。

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. 我要告诉您的是,将注释嵌入到后期文档中是一个“错误的想法(tm)”-由于性能原因,您希望限制文档的持续增长,因此最多可以将前几个注释嵌入文档本身。 (这样一来,您可以在显示帖子时显示前几条评论),但这些评论属于自己的收藏集,如果查看者要求查看更多评论,则可以在其中进行查询。 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. 请注意,当用户更改化身时,以非规范化形式更新化身将是异步的,但这没关系,因为用户不需要太多更改化身,您不必担心更新时间是否会长一点,但会在性能上获得巨大收益通过优化最频繁的操作-仅阅读一次即可在视图中显示帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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