Followers - mongodb database design

后端 未结 4 844
情话喂你
情话喂你 2020-12-14 12:19

So I\'m using mongodb and I\'m unsure if I\'ve got the correct / best database collection design for what I\'m trying to do.

There can be many items, and a user can

4条回答
  •  独厮守ぢ
    2020-12-14 12:59

    I read your comment/use-case. So I update my answer.

    I suggest to change the design as per this article: MongoDB Many-To-Many

    The design approach is different and you might want to remodel your approach to this. I'll try to give you an idea to start with. I make the assumption that a User and a Follower are basically the same entities here. I think the point you might find interesting is that in MongoDB you can store array fields and this is what I will use to simplify/correct your design for MongoDB.

    The two entities I would omit are: Followers and ItemGroups

    • Followers: It is simply a User who can follow Groups. I would add an array of group ids to have a list of Groups that the User follows. So instead of having an entity Follower, I would only have User with an array field that has a list of Group Ids.
    • ItemGroups: I would remove this entity too. Instead I would use an array of Item Ids in the Group entity and an array of Group Ids in the Item entity.

    This is basically it. You will be able to do what you described in your use case. The design is simpler and more accurate in the sense that it reflects the design decisions of a document based database.

    Notes:

    • You can define indexes on array fields in MongoDB. See Multikey Indexes for example.
    • Be wary about using indexes on array fields though. You need to understand your use case in order to decide whether it is reasonable or not. See this article. Since you only reference ObjectIds I thought you could try it, but there might be other cases where it is better to change the design.
    • Also note that the ID field _id is a MongoDB specific field type of ObjectID used as primary key. To access the ids you can refer to it e.g. as user.id, group.id, etc. You can use an index to ensure uniqueness as per this question.

    Your schema design could look like this:

    Schema design for MongDB (document database)

    As to your other question/concerns

    Is there a recommended maximum for array lengths before hitting performance issues anyway?

    the answer is in MongoDB the document size is limited to 16 MB and there is now way you can work around that. However 16 MB is considered to be sufficient; if you hit the 16 MB then your design has to be improved. See here for info, section Document Size Limit.

    I think with the following design a real performance issue could be when I want to get all of the groups that a user is following for a specific item (based off of the user_id and item_id)...

    I would do this way. Note how "easier" it sounds when using MongoDB.

    1. get the item of the user
    2. get groups that reference that item

    I would be rather concerned if the arrays get very large and you are using indexes on them. This could overall slow down write operations on the respective document(s). Maybe not so much in your case, but not entirely sure.

提交回复
热议问题