mongo db设计的以下和feed,我应该在哪里嵌入? [英] mongo db design of following and feeds, where should I embed?

查看:123
本文介绍了mongo db设计的以下和feed,我应该在哪里嵌入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的问题,我应该在mongo db中嵌入一组追随者/追随者。在用户对象中嵌入一个以下的嵌入式集合是有意义的,但是同样嵌入逆向追随者集合也是有意义的呢?这将意味着我必须更新和嵌入在以下嵌入式列表中

I have a basic question about where I should embed a collection of followers/following in a mongo db. It makes sense to have an embedded collection of following in a user object, but does it also make sense to also embed the converse followers collection as well? That would mean I would have to update and embed in the profile record of both the:


  1. 的配置文件记录中

  2. 以及followee的关注者嵌入列表

我无法确保原子性,除非我也以某种方式保持事务或更新状态。是值得嵌入在两个实体,还是应该更新#1,嵌入跟随者的个人资料,并在其上放置一个索引,以便我可以查询所有配置文件的反向关注者?性能是否太高了?

I can't ensure atomicity on that unless I also somehow keep a transaction or update status somewhere. Is it worth it embedding in both entities or should I just update #1, embed following in the follower's profile and, put an index on it so that I can query for the converse- followers across all profiles? Is the performance hit on that too much?

这是不应该嵌入的集合的候选者吗?我应该只有一个边缘的集合,我在其自己的收藏与followid和followbyId吗?

Is this a candidate for a collection that should not be embedded? Should I just have a collection of edges where I store following in its own collection with followerid and followedbyId ?

现在,如果我还必须更新两个用户的Feed

Now if I also have to update a feed to both users when they are followed or following, how should I organize that?

对于用例,用户在查看他们的Feed时会看到他们正在关注的人,这种情况很常见,并且当他们查看任何人的配置文件详细信息时,还可以查看配置文件的关注者,这也经常发生,但不像第一种情况那样多。

As for the use case, the user will see the people they are following when viewing their feeds, which happens quite often, and also see the followers of a profile when they view the profile detail of anyone, which also happens often but not quite as much as the 1st case. In both cases, the total numbers of following and followers shows up on every profile page.

推荐答案

一般情况下,这是一个坏主意将以下/后续关系嵌入用户文档中,原因如下:

In general, it's a bad idea to embed following/followed-by relationships into user documents, for several reasons:

(1)最大文档大小限制为16MB,订阅良好的网站的用户可能会遇到成千上万的关注者,这将接近最大文档大小,

(1) there is a maximum document size limit of 16MB, and it's plausible that a popular user of a well-subscribed site might end up with hundreds of thousands of followers, which will approach the maximum document size,

(2)跟踪关系经常变化,因此如果你嵌入了追随者,用户获得了很多追随者的情况转化为重复的文档增长。经常的文档增长将显着地阻碍MongoDB的性能,因此应该避免(偶尔的文档增长,特别是文档倾向于达到稳定的最终尺寸,更少的性能损失)。

(2) followership relationships change frequently, and so the case where a user gains a lot of followers translates into repeated document growth if you're embedding followers. Frequent document growth will significantly hinder MongoDB performance, and so should be avoided (occasional document growth, especially is documents tend to reach a stable final size, is less of a performance penalty).

所以,是的,最好把下面的/后面的关系分成一个单独的记录集合,每个记录都有两个字段,例如{_id:,oid:},索引在_id上我跟着?查询)和oid(对于谁在跟着我?查询)。任何单独的状态更改都是通过单个文档添加或删除来建模的,但如果您还显示关注者数量等信息,则应该保留在任何边缘插入/删除后更新的单独计数器。

So, yes, it is best to split out following/followed-by relationship into a separate collection of records each having two fields, e.g., { _id : , oid : }, with indexes on _id (for the "who am I following?" query) and oid (for the "who's following me?" query). Any individual state change is modeled by a single document addition or removal, though if you're also displaying things like follower counts, you should probably keep separate counters that you update after any edge insertion/deletion.

(当然,这假设你的业务需求允许你在一致性细节上有一些灵活性:一般来说,如果你的显示代码告诉用户他有304个追随者,然后继续枚举它们,只有最困惑用户将检查追踪者是否枚举到304.如果业务要求需要绝对一致性,你需要一个数据库隔离你的交易,否则你必须做自己计数,作为显示所有用户身份的一部分。)

(Of course, this supposes your business requirements allow you some flexibility on the consistency details: in general, if your display code tells a user he's got 304 followers and then proceeds to enumerate them, only the most fussy user will check that the followers enumerated tally up to 304. If business requirements necessitate absolute consistency, you'll either need a database that isolates transactions for you, or else you'll have to do the counting yourself as part of displaying all user identities.)

这篇关于mongo db设计的以下和feed,我应该在哪里嵌入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆