I am setting up a website that will have users. Users can search for songs and (if signed in) save them to a playlist or their library of songs.

I want it to be like iTunes in that all the songs on the the playlists are part of the library so when the user choses to view their library of songs, the songs on the playlists are shown as well as the songs just added to the library. I'd like a song to be able to be stored in multiple playlists.

Right now I am using Mongodb, doing something like such:

var UserSchema = new Schema();

var Library = new Schema({
    songs: [SongsSchema],
    user: Schema.ObjectId

var Playlist = new Schema({
title: String,
description: String,
user: Schema.ObjectId   

var SongsSchema = new Schema({
position: Number,
name: String,
artist: String,
    artistGid: String,
album: String,
    albumGid: String
time: Number,
url: String,
    gid: String,
    location: String (either youtube, vimeo, soundcloud for now),
    playlist: [Schema.ObjectId] (array of object ids that point to playlists?)

Any other ideas?

edit: More details:

So what I have is a user, library, playlist, and song model.

Essentially I am trying to model something like iTunes.

A user has a library and the library belongs to that user. A user can have several playlists and the playlist belongs to the user. A song can be in many different user libraries and many different playlists that belong to different users or the same user (perhaps even on the same playlist multiple times?).

When a user visits a page they can add songs to a playlist and will also see a list of their current playlists and their library. If they click the playlist it will show all the songs on that playlist if they click library it will show all the songs in their library( so like iTunes all the songs on all the playlists and ones just added to the library).

Not sure of the best way to model this.

  • 3
    Is there a particular reason you want to use Mongodb for this application? What you're designing is very straightforward in a relational database. You can do it in non-relational but I don't see why you would want to. Is there more to the system you haven't mentioned which cries out for non-relational?
    – Joel Brown
    Jan 14, 2012 at 14:18
  • Thanks. Ya after messing around I think it might be useful. I will edit my post describing it in more detail and hopefully you can help. I want it to be quick because I am expecting lots of read and writes.
    – Raladical
    Jan 16, 2012 at 20:48
  • 4
    You're optimising for a problem you don't have yet. Wait until you actually need to before making things more difficult for yourself. Jan 16, 2012 at 21:09
  • 2
    Agreed with Simon. RDBMS is screamingly fast for relationally structured data like this. If you stop thinking in objects and start thinking in relations then you will see how efficient normalized data would be. I'll scratch up a quick ERD to show you what it would look like.
    – Joel Brown
    Jan 17, 2012 at 2:57

1 Answer 1


NOTE to people with itchy downvote fingers: I know that OP has asked about MongoDB and the answer that follows is RDBMS. However, if you check out the comments you'll see that I did ask why MongoDB and OP's answer is a presumption of necessity due to performance. Since nobody has come forth with a Mongo-centric answer in four days, I am going to offer my suggestion, which is to let RDBMS do what it's good at.

Raladical, as I noted in my comments to your question, I think that what you are trying to build is perfectly suited to the relational data model. Here is a quick data model sketch:

Data Model Sketch

This model makes the following assumptions:

  • Users can have exactly one library.
  • Users can have many playlists.
  • Songs can appear in any number of playlists.
  • Users can only put songs in a playlist if it's already in their library (Note that there is an easy way around this if that is not your intention.)
  • Depending on the constraints you put on LIBRARY_ITEM and PLAYLIST_ITEM you can have the same song exactly once or several times in both the library and any given playlist.

You can also easily extend this model to normalize for things like ARTIST and possibly even location, if that makes sense in your application.

This model is normalized (3NF) and if you index it properly it will be very performant. If you really want MongoDB or if there is something else about your system that is well suited to non-relational then you can definitely do it that way. If not, you should think about RDBMS for this one.

  • This looks awesome! Thanks so much for writing this up. I will try this out. And yeah I am looking to be able to add songs to a playlist but if that is done it is also automatically part of the library.
    – Raladical
    Jan 17, 2012 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.