如何使用MongoDB跟踪私人消息传递系统? [英] How to keep track of a private messaging system using MongoDB?

查看:52
本文介绍了如何使用MongoDB跟踪私人消息传递系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用facebook的私人消息传递系统,您必须在其中跟踪消息内容的发送者和接收者.如果我使用MySQL,则将有多个表,但是使用MongoDB时,我将尽量避免所有这些表.我正在尝试提出一个可扩展且易于维护的良好"架构.如果我使用的是mysql,则将有一个单独的表来引用用户和消息.见下文...

Take facebook's private messaging system where you have to keep track of sender and receiver along w/ the message content. If I were using MySQL I would have multiple tables, but with MongoDB I'll try to avoid all that. I'm trying to come up with a "good" schema that can scale and is easy to maintain. If I were using mysql, I would have a separate table to reference the user and and message. See below ...

个人资料表

user_id
first_name
last_name

消息表

message_id
message_body
time_stamp

user_message_ref表

user_message_ref table

user_id (FK)
message_id (FK)
is_sender (boolean)

使用上面列出的架构,我可以查询鲍勃"可能拥有的任何消息,无论他是收件人还是发件人.

With the schema listed above, I can query for any messages that "Bob" may have regardless if he's the recipient or sender.

现在如何将其转换为可与MongoDB配合使用的架构.我想我将有一个单独的集合来保存消息.问题是,如何区分发送者和接收者?如果Bob登录,我要查询什么?取决于鲍勃是否发起电子邮件,我不想只查询发件人"和收件人",而不必查看邮件是否属于用户.

Now how to turn that into a schema that works with MongoDB. I'm thinking I'll have a separate collection to hold the messages. Problem is, how can I differentiate between the sender and the recipient? If Bob logs in, what do I query against? Depending on whether Bob initiated the email, I don't want to have to query against "sender" and "receiver" just to see if the message belongs to the user.

我查看了MongoDB的消息组,并发现了可能有效的方法. 每个消息将被视为博客"帖子.创建消息后,将两个用户(与最初的发送者/接收者无关)添加到数组中.此后的每个响应都将被视为注释,并将其插入到数组中.

I hit up MongoDB's message group and came away with something that may work. Each message would be treated as a "blog" post. When a message is created, add the two users (doesn't matter who sender/receiver is initially) into an array. Each response after that would be treated as a comment, which would be inserted into an array.

{
    "_id" : <objectID>,
    "users" : ["bob", "amy"],
    "user_msgs" :
        [
            { 
                "is_sender" : "bob",
                "msg_body" : "Hi Amy, how are you?!",
                "timestamp" : <generated by Mongo>
            }
            { 
                "is_sender" : "amy",
                "msg_body" : "Bob, long time no see, how is the family?!",
                "timestamp" : <generated by Mongo>
            }
        ]
}

这样,我可以查询涉及鲍勃"的消息,并循环遍历"user_msgs"数组.我将能够分辨出发件人是谁,并按时间戳排序.

This way I can query for messages that involves "Bob," and loop through the "user_msgs" array. I'll be able to tell who the sender is and sort by the timestamp.

推荐答案

弄清楚了.参见我在原始帖子中的解释.

Figured it out. See my explanation above in original post.

这篇关于如何使用MongoDB跟踪私人消息传递系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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