0

I'm working on a snapchat clone. I'm using mongoDB to store meta data and urls for the pictures and videos (Stored on cloudinary). The main issue that I am having is with modeling the stories feature. I currently have two schemas declared, one Stories schema and one Videos schema.

When a video document is added to a NEW story:

  1. A new Story document is created.
  2. A new Video document is created.
  3. The objectId of the Story document is stored on a field in the Video document.

When a video document is added to an EXISTING story:

  1. A new Video document is created.
  2. The objectId of the Story document is stored on a field in the Video document.

To receive a list of stories to watch the api queries the Stories collection for all available stories. The user can then select which story they wish to view. The objectId from the chosen story is used to query the Videos collection for all of the documents that contain the specified story's objectId. All documents in both the Video and Story collections are set to expire after 86400 seconds (24 hours).

Ideally every time a new video is added to a story the expiration time should be reset to a full 86400 seconds. I could not figure out a good way of resetting the expiration time so I used mongo's findOneAndReplace method to find the story and replace it with a brand new copy of itself, therefore "resetting" the expire time. I'm adding functionality that lets users see who liked their stories which means I had to add first name, last name, and objectId for each liker. While findOneAndReplace worked perfectly with small document sizes, as the documents grow larger the time needed for the operation goes up. Is there a better way to reset the TTL on the Stories collection without recreating each document every time?

TLDR Is there a way to reset the expire time on a MongoDB document without creating a new document?

TheNewGuy
  • 85
  • 2
  • 12
  • TTL expiration is based on an indexed date field, so you can always update the value of the date field if you want to change when a specific document expires. – Stennie Dec 05 '16 at 04:40
  • If this is a duplicate of an existing question would you mind sending the link to the other question? @Stennie Never mind i see the link. Thanks – TheNewGuy Dec 05 '16 at 04:49
  • @Stennie I'm still slightly confused by that answer. Am i correct in saying that if i set createdAt: { type: Date, index: true, default: Date.now, expires: 86400 } originally, in order to update the document i simply set default to Date.now again?? – TheNewGuy Dec 05 '16 at 14:07
  • Yes, that's correct. The TTL index works by searching for documents with a date older than `Date.now()`-`expireAfterSeconds` every 60s. If you update the date field indexed for TTL to `Date.now()` you are resetting the expiry by `expireAfterSeconds`; you could also set to a date in the past to have the document eligible for expiry in the next TTL pass. An alternative usage of TTL is to set `expireAfterSeconds` to a value of `0`, in which case the indexed date value indicates which time each document will expire (so you can have varying per-document expiry without extending the date). – Stennie Dec 05 '16 at 19:23
  • For more information see [Expire Data from Collections by Setting TTL](https://docs.mongodb.com/manual/tutorial/expire-data/), which is also referenced in the duplicate question. – Stennie Dec 05 '16 at 19:23

0 Answers0