MongoDB:将嵌入式用户纳入注释 [英] MongoDB: Embedded users into comments

查看:95
本文介绍了MongoDB:将嵌入式用户纳入注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于非常简单的问题(或不是非常简单的问题),我找不到最佳"解决方案

I cant find "best" solution for very simple problem(or not very)

具有经典的数据集:附加到用户的帖子,附加到帖子和用户的评论.

Have classical set of data: posts that attached to users, comments that attached to post and to user.

现在我无法决定如何构建方案/类

Now i can't decide how to build scheme/classes

一种方式是将user_id存储在注释和内部.
但是,当我在页面上有200条评论时会发生什么?
还是页面上有N个帖子?
我的意思是应该向数据库发送200个其他请求以显示用户信息(例如姓名,头像)

On way is to store user_id inside comments and inside.
But what happens when i have 200 comments on page?
Or when i have N posts on page?
I mean it should be 200 additional requests to database to display user info(such as name,avatar)

另一种解决方案是将用户数据嵌入每个评论和每个帖子中.

Another solution is to embed user data into each comment and each post.

但是首先->这是巨大的开销,其次->模型系统已损坏(使用mongoalchemy),第三->用户可以更改其信息(例如头像).那又如何呢?据我了解,对大量评论或帖子进行更新操作不是简单的操作...

But first -> it is huge overhead, second -> model system is getting corrupted(using mongoalchemy), third-> user can change his info(like avatar). And what then? As i understand update operation on huge collections of comments or posts is not simple operation...

您有什么建议?每页对mongodb的200个请求是否可以(必须以性能为目标)?

What would you suggest? Is 200 requests per page to mongodb is OK(must aim for performance)?

或者我只是想念一些东西...

Or may be I am just missing something...

推荐答案

您可以使用

You can avoid the N+1-problem of hundreds of requests using $in-queries. Consider this:

Post {
  PosterId: ObjectId
  Text: string
  Comments: [ObjectId, ObjectId, ...] // option 1
}

Comment {
  PostId: ObjectId // option 2 (better)
  Created: dateTime,
  AuthorName: string,
  AuthorId: ObjectId,
  Text: string
}

现在,您可以通过$in查询找到帖子评论,还可以轻松找到特定作者发表的所有评论.

Now you can find the posts comments with an $in query, and you can also easily find all comments made by a specific author.

当然,您也可以将注释作为嵌入式数组存储在post中,并在获取注释时对用户信息执行$in查询.这样,您就无需取消用户名的规范化,并且仍然不需要数百个查询.

Of course, you could also store the comments as an embedded array in post, and perform an $in query on the user information when you fetch the comments. That way, you don't need to de-normalize user names and still don't need hundreds of queries.

如果您选择对用户名进行非规范化,则当用户更改(例如)时,必须更新该用户曾经发表过的所有注释.他的名字.另一方面,如果这样的操作不经常发生,那也没什么大不了的.或者,最好根据用户的要求存储用户发表评论时使用的名称.

If you choose to denormalize the user names, you will have to update all comments ever made by that user when a user changes e.g. his name. On the other hand, if such operations don't occur very often, it shouldn't be a big deal. Or maybe it's even better to store the name the user had when he made the comment, depending your requirements.

嵌入的一个普遍问题是

A general problem with embedding is that different writers will write to the same object, so you will have to use the atomic modifiers (such as $push). This is sometimes harder to use with mappers (I don't know mongoalchemy though), and generally less flexible.

这篇关于MongoDB:将嵌入式用户纳入注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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