如何在 mongodb 中设计作者/用户模型的架构
我浏览了 mongo 网站上的大多数 mongodb 模式设计文章以及这里的大多数问题。还有一个用例我还没有弄清楚。在查看这些教程时,他们通常会参考文章评论问题和产品/类别问题。我想弄清楚在查询帖子列表时如何对一对多关系(作者到帖子)进行建模。以下是示例架构:
Users: {
_id: ObjectID
Name: String
Email: String
}
Posts: {
_id: ObjectID
user_id: ObjectID
body: String
comments: [
body: String
]
}
现在,假设您要运行对最新 10 篇帖子的查询。这是一个非常简单的查询,但现在您的帖子可能每个帖子都有一个指向用户的唯一 ObjectID。现在,您应该如何获取帖子的每个用户的姓名和电子邮件。
是否应该从 posts 查询创建用户 ObjectID 的数组,然后运行查询 db.users.find({ _id: {$in: PostsUserIDArray}});之后,您会使用应用程序逻辑将正确的用户信息与正确的帖子相匹配吗?
您是否应该保留帖子中数据的副本。 IE 将用户 ID、姓名和电子邮件保留在 posts 表中。然后,当用户更新此信息时,只需挂钩即可更新帖子中的所有信息。
我自己或我的朋友没有想到的选项。
当我尝试了解 mongo 数据建模时,我感谢所有帮助。
I have looked through most of the mongodb schema design articles on mongo's website and most of the questions here on SO. There is still one use case which I haven't figured out. When looking at these tutorials, they usually reference the article comments problem and the products/categories problem. I want to figure out how to model the one to many relationship (author to posts) when querying a list of posts. Here are the example schemas:
Users: {
_id: ObjectID
Name: String
Email: String
}
Posts: {
_id: ObjectID
user_id: ObjectID
body: String
comments: [
body: String
]
}
Now, lets say you want to run a query for the latest 10 posts. A pretty simple query, but now you have posts with the possibility of each one having a unique ObjectID pointing to the user. Now, how should you accomplish getting the name and email of each user for a post.
Should you create an array of the user ObjectID's from the posts query and then run the query db.users.find({ _id: {$in: PostsUserIDArray}}); After that would you use your application logic to match the right user information to the correct post?
Should you keep a copy of the data in posts. I.E. keep the user ID, name, and email in the posts table. Then just have a hook when a user updates this information to update all the information in posts.
An option which myself or my friend have not thought of.
I appreciate all help as I try to wrap my head around mongo data modeling.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看到的 MongoDB 创建者的一些视频中,他们提倡第二种解决方案。如果您的用户拥有的数据不仅仅是姓名和电子邮件,并且您在显示帖子时仅显示姓名和电子邮件,那么将其存储在帖子中也不错。因此,您在查询帖子时不必执行其他查询。由于用户通常不会每天更改其姓名,因此在更改姓名后对所有帖子进行更新比在显示帖子时执行其他查询来检索信息更有效。
编辑:链接到视频 http://lacantine.ubicast.eu/videos/ 3-mongodb-部署策略/
For a few videos I have seen from MongoDB creators, they advocate the second solution. If your user have more data than just a name and email and if you display only name and email when displaying apost, then it's not really bad to store it in the post. Thus you don't have to perform others queries when querying for posts. And since a user doesn't normally change his name every day, it's more effective to run an update to all posts once he changes his name than perform other queries to retrieve informations when displaying posts.
Edit : link to a video http://lacantine.ubicast.eu/videos/3-mongodb-deployment-strategies/