Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to keep track of a private messaging system using MongoDB?
    primarykey
    data
    text
    <p>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 ...</p> <p>profiles table</p> <pre><code>user_id first_name last_name </code></pre> <p>message table</p> <pre><code>message_id message_body time_stamp </code></pre> <p>user_message_ref table</p> <pre><code>user_id (FK) message_id (FK) is_sender (boolean) </code></pre> <p>With the schema listed above, I can query for any messages that "Bob" may have regardless if he's the recipient or sender.</p> <p>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.</p> <p>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.</p> <h1>MESSAGES</h1> <pre><code>{ "_id" : &lt;objectID&gt;, "users" : ["bob", "amy"], "user_msgs" : [ { "is_sender" : "bob", "msg_body" : "Hi Amy, how are you?!", "timestamp" : &lt;generated by Mongo&gt; } { "is_sender" : "amy", "msg_body" : "Bob, long time no see, how is the family?!", "timestamp" : &lt;generated by Mongo&gt; } ] } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload