2

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 ?

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
ParparDromi
  • 145
  • 2
  • 8

2 Answers2

0

I'm not familiar with MongoDB, but wouldn't it make more sense to change your 'play' document to something like:

{ "user" => "user mongoid", "track" => "track mongoid", "plays" => "number of plays" }

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.

Community
  • 1
  • 1
John
  • 691
  • 3
  • 8
0

I would suggest moving the track history to another collection. You are correct that an ever going document in a collection is not ideal (Although the max document size is now 16MB).

Do you need to maintain a history of everything or just the last 20? If it's just the last 20 then maybe you can keep it embedded.

I would also recommend calculating the number of plays in your app by doing an increment ($inc) in a similar manner to that outlined by John (answer above). Then you won't have to do a calculation with a count() which could be slow.

{'user':'<user>', 'track':'<track>','plays':1}

Then use something like this to update

db.col.update({'user':'aUser'},{$inc : {'plays' : 1}});