I would like to implement a feature as in youtube where you know which videos you have seen.
I have to solutions:
What you might suggest?
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.