简体   繁体   English

MongoDB 文档设计决策

[英]MongoDB design decision for Documents

I'm building an API for a small social network and I came across a design decision that I have to make.我正在为一个小型社交网络构建一个 API,我遇到了一个我必须做出的设计决定。 I'm working with Express and MongoDB with mongoose to deal with the database.我正在使用 Express 和 MongoDB 和 mongoose 来处理数据库。

I have two Documents: Users and Posts.我有两个文档:用户和帖子。 I want the Users to be able to mark Posts as their favorites.我希望用户能够将帖子标记为他们的收藏夹。 I came up with two different ways for the Implementation:我想出了两种不同的实现方式:

Option A: Saving the favorites in the User Document.选项 A:将收藏夹保存在用户文档中。 It makes it easy to show all favorite posts of an user.它可以轻松显示用户所有喜欢的帖子。 But how would I query the users, that have favorited a specific Post?但是我将如何查询喜欢特定帖子的用户?

UserSchema:
   favorite_posts: [
      {
         type: mongoose.Schema.Types.ObjectId,
         ref: "posts"
      }
   ]

Option B: Saving the Users, that hit the favorite button in the Post Document.选项 B:保存用户,点击发布文档中的收藏按钮。 The benefit would be, that you can easily display all Users, that have favorited a Post.这样做的好处是,您可以轻松显示所有收藏帖子的用户。 But how do I list all Posts that one specific User has marked as favorites.但是我如何列出一个特定用户标记为收藏的所有帖子。

PostSchema:
   users_favorited: [
      {
         type: mongoose.Schema.Types.ObjectId,
         ref: "users"
      }
   ]

Can somebody explain me how to query such things?有人可以解释我如何查询这些东西吗? I'm not getting smarter from the documentation... :(我没有从文档中变得更聪明...... :(

As already mentioned in the comments your best bet would be a join-table to make an:m relation work.正如评论中已经提到的那样,您最好的选择是连接表以使:m 关系工作。 Mongoose does emulate the sql inner-join functionality through the populate() functionality in regular queries or the $lookup-step in an aggregation. ZCCADCDEDB567ABAE643E15DCF0974E503Z 确实通过常规查询中的 populate() 功能或聚合中的 $lookup-step 来模拟 sql 内连接功能。 So basically create a table called "likes" that only holds refs to the user and the post.所以基本上创建一个名为“likes”的表,它只包含对用户和帖子的引用。 Using the aggregation framework, you can then easily query for all likes of a user or all likes on a post by first using the $match operator, then $group by either the user or the post and $push to create an array of all likes of a user or vice versa and then join the needed data on it using the $lookup step.使用聚合框架,您可以轻松查询用户的所有喜欢或帖子上的所有喜欢,首先使用 $match 运算符,然后按用户或帖子的 $group 和 $push 创建所有喜欢的数组用户或反之亦然,然后使用 $lookup 步骤加入所需的数据。

However, you could, as you've decribed, put all the favorites in a array on either the user- or the post-documents, but unless you know for sure that these arrays won't grow large, I'd recommend against it, as mongoDb is not designed for this kind of usage and you'll very quickly run into performance problems.但是,正如您所描述的,您可以将所有收藏夹放在用户文档或发布文档的数组中,但除非您确定这些 arrays 不会变大,否则我建议您不要这样做,因为 mongoDb 不是为这种用途而设计的,您很快就会遇到性能问题。 See http://www.askasya.com/post/largeembeddedarrays/ for more.有关更多信息,请参阅http://www.askasya.com/post/largeembeddedarrays/

If you are gonna query a lot by userid, you can just add a userid column on the favorites document.如果您要通过 userid 查询很多,您可以在收藏夹文档中添加一个 userid 列。 This would save queries/joins/aggregations这将节省查询/连接/聚合

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

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