Reputation: 20538
I am developing a webapp using PHP and MongoDB. In this app I keep two collections. One to keep data about files and one to keep track about download events for each file.
I need to get the 10 latest downloaded files but I know joins is not an option. The events document only stores the file id and the other collection stores the thumbnail.
Right now I first get the 10 recently downloaded files and order it by date and the order is fine but then I use this array of files (their ids) and make a where_in query where I look for files whos id is present in the ids array. This also works fine (I get the thumbnails for the selected files) but I cannot keep the order anymore. The most recently downloaded file is not longer on top.
How can I keep the order of the ids without looping through them thus making 10 new queries instead of just one?
I cannot change the schema because I got over 40.000 documents :)
UPDATE
In short this is what I want to do:
The above steps works fine, BUT I cannot keep the order from the first step thus I cannot get the most recently downloaded file on top. I know I could look trought the id array and get data for each file but that would cost me 10 queries instead of one.
Upvotes: 0
Views: 286
Reputation: 230461
I don't really get your problem. Here's some pseudo-code:
// get last N download events
events = get_latest_downloads()
// retrieve associated file data
file_array = get_all_file_info_by_events(events)
display_data = []
for(e in events) {
data = find_file_info_in_the_array(file_array, e.file_id)
display_data.push(data)
}
// now display_data is contains full file info, sorted by download time.
Upvotes: 1