Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hmm, this is a tricky one!</p> <p>It makes sense to me to store a record for entries that are unread, and delete them when they're read. I'm basing this on the assumption that there will be more read posts than unread for each individual user, so you might as well not have documents for all of those already-read entries sitting around in your DB forever. It also makes it easier to not have to worry about the 16MB document size limit if you're not having to drag around years of history with you everywhere.</p> <p>For starred entries, I would simply add an array of Entry ObjectIds to User. No need to make these subscription-specific; it'll be much easier to pull a list of items a User has starred that way.</p> <p>For unread entries, it's a little more complex. I'd still add it as an array, but to satisfy your requirement of being able to quickly mark as-read entries before a specific date, I would denormalize and save the publish-date alongside the Entry ObjectId, in a new 'UnreadEntry' document.</p> <pre><code>User fields: email_address, starred_entries[] subscriptions (embedded collection; fields: feed_id, tags, unread_entries[]) UnreadEntry fields: id is Entry ObjectId, publish_date </code></pre> <p>You need to be conscious of the document limit, but 16MB is one hell of a lot of unread entries/feeds, so be realistic about whether that's a limit you really need to worry about. (If it is, it should be fairly straightforward to break out User.subscriptions to its own document.)</p> <p>Both of your queries now become fairly easy to write:</p> <p>All entries for a specific feed that are unread: <code>user.subscriptions.find(feedID).unread_entries</code></p> <p>Mark all entries prior to a publish date read: <code>user.subscriptions.find(feedID).unread_entries.where(publish_date.lte =&gt; my_date).delete_all</code></p> <p>And, of course, if you simply need to mark all entries in a feed as read, that's very easy: <code>user.subscriptions.find(feedID).unread_entries.delete_all</code></p>
 

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