i'm trying to figure out a way to record a user plays without slowing down my application just like iTunes.
my application is basically a music website its heavily integrated with facebook, facebook users can log in and approve my application and start listening to songs and make their own playlists etc..
the application is built using php
and mongodb
.
i tried adding this feature 2 times already but each method has its own flaws, i just want to see what you "pros" think because i have only 1 year experience in building applications.
method 1
make a collecton named plays
and each document will look like this.
{
"user" => "user mongoid",
"track" => "track mongoid"
}
the problem with method 1
the problem with this one is that it will be very slow, because when i display the artist page i will have to list the 20 most recent tracks, then when getting the tracks i will have to check for each track how much the current user listened to it.
so after getting the tracks i will have to this to get the current user plays:
// getting the tracks.
$cursor = $this->tracks->find();
$tracks = array();
foreach ($cursor as $t) {
// getting the number of plays.
$t['plays'] = $this->plays->find(array('user_id' => $user, 'track_id' => $t['_id']))->count();
$tracks[] = $t;
}
method 2.
embed plays in the user document. this method seemed ok and it was pretty fast, when a user plays a track just add or increment the number of tracks for this track. and then just display them.
the problem with this method. MongoDB allows only 4 mb for document. i cant just push everything in the user document.
i looked all over the net for someone who ran into similar problem with mongo but i didnt find any.
So how can i record per user plays without making the application slower ?