Reputation: 3425
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?)
});
Does this seem the best? I am used to relational so it seems like there is lots of duplication but I was having a hard time normalizing it in a way that would work.
Upvotes: 1
Views: 1181
Reputation: 844
Looks reasonable to me. Though in this design you can't support ordered playlists. I would include both an ObjectId for the playlist and the position in the playlist in the playlist array if you want to support that feature.
Upvotes: 2
Reputation: 30136
welcome to Mongo!
That sounds good, just make sure you have an index on songs.playlist
db.collection.ensureIndex({"songs.playlist": 1})
Might make sense to make libraries / playlists the same thing, just have the title of the library object be "Library"
Upvotes: 1