Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are going to need some kind of link between the two collections (users and messages).</p> <p>Personally, I would keep it simple and add two extra fields to track the id of the sender and recipient, something like this:</p> <pre><code>{ _id: /* whatever_id */, message_body: "This is the message", date_sent: 2010-04-20T10:35, sender_id: /*id_of_sender*/, recipient_id: /* id_of_recipient */ } </code></pre> <p>The <code>sender_id</code> and <code>recipient_id</code> fields would just hold value for the appropriate user (most likely some <a href="http://www.mongodb.org/display/DOCS/Object+IDs" rel="nofollow noreferrer">ObjectID</a> instance, although you can assign whatever you like) which corresponds to the _id field for the appropriate entries in the users collection. You would be able to query these appropriately to grab the messages you are after (or count them, or whatever else).</p> <p>Another approach might be to effectively do the same thing, but to use a formal <a href="http://www.mongodb.org/display/DOCS/Database+References" rel="nofollow noreferrer">DBRef</a> for the sender and recipient rather than just putting their IDs in. This would probably work just as well but I'd tend to go with the previous solution just because it is simpler and probably easier to query.</p> <p>Both solutions would need to do another round-trip to the DB to grab the appropriate user documents (for displaying the "from" and "to" names for example).</p> <hr> <p><strong>Edit:</strong><br> It would appear I have misunderstood what you are trying to achieve - I didn't know Facebook messaging incorporated any concept of threading. However, the solution you have presented above looks sound. Personally, I'd stick in the IDs for the users rather than their names (alice &amp; bob), but it looks pretty workable apart from that.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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