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 ?
I’m not familiar with MongoDB, but wouldn’t it make more sense to change your ‘play’ document to something like:
That way you don’t have to do a count when you display the artist overview. I suspect MongoDB can do very fast lookups/updates, but based on other questions (like MongoDB's performance on aggregation queries) aggregation seems to be fairly slow.