How to make a high performance select of 10 items more or less and then know whenever the current user already viewed the item in mongodb? [closed]

StackOverflow https://stackoverflow.com//questions/23047119

Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top