Listing an image gallery based on ranking

I'm creating a website with a feed of images ( similar to pinterest). I want to list those images in the ascending order of their popularity ( no of likes and views each images receives) with pagination.

Im confused on how to implement this ..

We are using mongoDB, Document has the following fields 1. _id 2. user_id 3. image_id 4. score ( which is value of popularity,( no of views) + 10x (no of likes)) 5. Rank

We are thinking of running a script daily to calculate the value of score. And run a loop script to assign the rank ( image with highest score get 1st rank, second highest gets rank 2 etc).

The feed then fetches the images in the ascending order of the rank. Running such a script of millions of such documents will take a lot of time. Is there a better way of doing this?

1 answer

  • answered 2017-01-11 14:19 Alexey Obukhov

    you can try different approaches for your needs, implement them and find the best.

    1. your idea: background job (at night) to calculate field in db
    2. on update page: select images, order by likes*10 + views desc
    3. on every image view or like, update image rank