I am trying to update a mongoose model. Having some difficulites. This is how my schema looks like right now.
I want a user to have a library which consists of many songs. Those songs can be in multiple users libraries. And can also be in multiple playlists of which a user can have several.
var LibrarySchema = new Schema({
user: Schema.ObjectId
});
var PlaylistSchema = new Schema({
title: String,
description: String,
//songs: [SongsSchema],
user: Schema.ObjectId
});
var SongsSchema = new Schema({
name: String,
artist: String,
album: String,
time: Number,
url: String,
gid: String,
location: String,
playlist: [{playlist_id:Schema.ObjectId, position: Number}],
library: [Schema.ObjectId]
});
Now I want to do something like check if the url already exists in the song schema (url is an index). If that song does not exist I want to add it with all the values. However, if the song exists I want to append the playlist and position its in and the library.
For example here is the query I am trying now:
Song.update({url: s.url, "playlist.playlist_id": playlistId}, {$set: {name: s.name, artist: s.artist, album: s.album, time: s.time, url: s.url}, $addToSet: {playlist: {playlist_id: playlistId, position: s.position}, library: libId}},{upsert: true}, function(err, data) {});
And this is a sample of the database stored:
{ "name" : "Kanye West - All Of The Lights ft. Rihanna, Kid Cudi", "artist" : "unknown", "album" : "unknown", "time" : 328, "url" : "HAfFfqiYLp0", "_id" : ObjectId("4f127923ce0de70000000009"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
"playlist_id" : ObjectId("4f127923ce0de70000000007"),
"position" : "1"
}
] }
{ "name" : "Kanye West - Heartless", "artist" : "unknown", "album" : "unknown", "time" : 221, "url" : "Co0tTeuUVhU", "_id" : ObjectId("4f127923ce0de7000000000a"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
"playlist_id" : ObjectId("4f127923ce0de70000000007"),
"position" : "2"
}
] }
{ "name" : "Kanye West - Stronger", "artist" : "unknown", "album" : "unknown", "time" : 267, "url" : "PsO6ZnUZI0g", "_id" : ObjectId("4f127923ce0de7000000000b"), "library" : [ ObjectId("4f1203af23c98cfab1000006") ], "playlist" : [
{
"playlist_id" : ObjectId("4f127923ce0de70000000007"),
"position" : "3"
}
] }
Now what I am trying to do is if the url for the song is not in the database it should add it with all the information. If it is not then it will add to the library (which each user has one of) the library id UNLESS it already contains that value.
That works pretty good. The problem I am having right now is when it finds the url I want it to add to playlist the playlistId of the current playlist UNLESS it is already in the array and if it is and was passed a new value for position (user can change the order of a playlist) it should change the value of the position.
Perhaps I am not modelling it in a proper way. Any tips would be awesome!
MongooseJS contains something called DBRef. I think you want to use that, instead of handling all the references yourself.
For inserting if something is not there, either do a lookup first and insert if needed, or use an upsert. Not sure if Mongoose supports Upserts though…