0

I would like to implement a feature as in youtube where you know which videos you have seen.

I have to solutions:

  • Store the videos played in the local storage and use this data to update the user interface. (This will require a high data load on start if the data is not already in the locastorage and the user has viewed a lot of videos)
  • Store data in a table, of course, and after you get the results of the query, get each video information that is related to the current user. This way would require too many queries in my opinion.

What you might suggest?

Totty.js
  • 15,563
  • 31
  • 103
  • 175

1 Answers1

1

Store data in a table, of course, and after you get the results of the query, get each video information that is related to the current user. This way would require too many queries in my opinion.

I don't see how that requires a lot of queries, you only need two: one 'regular', one using $in:

Assuming a collection videos { _id, title, description, uploader, date, ... } and a collection userRecentVideos { userId, videoIds : [1, 912, 234234, ... ] },

this will do:

var recent = db.userRecentVideos.find({"userId" : myUserId})[0].videoIds;
var videoInfo = db.videos.find({"_id" : {$in : recent}});

Alternatively, you can expose a getVideoInfos HTTP endpoint that accepts a list of Ids and returns a list of video meta infos.

The videoIds should have a fixed size and should be initialized to all 0s or something so the array doesn't grow all the time.

If you really want to store the entire history, it would be better to not use an embedded array but make this a linker collection like so:

videoHistory { _id, userId, videoId }

Put an index on {_id, userId}, and you can get the n most recently played videos for each user, assuming _id is a monotonic key such as an object id.

mnemosyn
  • 45,391
  • 6
  • 76
  • 82
  • In your first statement "The videoIds should have a fixed size and should be initialized to all 0s or something so the array doesn't grow all the time.", would not work because it will have a variable number of videos. But I could only store the last 1000 videos, for example, I don't know, would this too much or is bad? How would be the performance for this? – Totty.js Apr 14 '14 at 13:14
  • .. and further down it says "If you really want to store the entire history, it would be better to not use an embedded array but make this a linker collection like so". No, that wouldn't hurt performance too badly at all. Just make sure you're querying reasonable amounts of data from your server and perform paging using the `_id` – mnemosyn Apr 14 '14 at 13:19
  • What does mean "perform paging using the" in this context? – Totty.js Apr 14 '14 at 13:31
  • if your collection grows huge, don't page using `skip`, but do a `$gt` on the `_id` (which must be monotonic to do so). That only is an issue if you page beyond thousands of documents. – mnemosyn Apr 14 '14 at 13:34
  • But the problem is that the collection is not sorted by id, but by a internal ranking system... – Totty.js Apr 14 '14 at 13:39
  • ok, doesn't really matter as long as you have a monotonic key (which is the sense of a ranking system in the first place). No worries. – mnemosyn Apr 14 '14 at 13:58