I am creating an API using PHP and MongoDB. In this system I got users and each user can upload files. Users can also "follow" each other.
I need to return a feed of all the latest files uploaded by users that the authenticated user is following. I am not really sure how to design and execute this.
This is what I am thinking of implementing.
- Get all the users that the user is following.
- Loop through this array of users and get latest 4 files for each user and add them to an array.
- Somehow order these files (this array) by creation date.
- Return it.
Is this really the optimal way? Is there a better way? Users are saved in the collection Users and files in the collection files. Following is saved in the collection users.followers.