Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7061439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:28:28+00:00 2026-05-28T04:28:28+00:00

I am trying to update a mongoose model. Having some difficulites. This is how

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:28:29+00:00Added an answer on May 28, 2026 at 4:28 am

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

am trying to update my database table that looks like this name | file_name
I am trying to update the Django User model with the class based UpdateView
Im trying to update someone else's Rails app. Right now, an HTML table displays
I am trying to update the document.title with Javascript. This works fine, but now
I trying to update a model on a callback but the validation is causing
Hi I am trying to update a div with some html from another div.
Im trying to update some nested params from a form. I can see that
I'm trying to update a model with nested properties after an edit. The update
Trying to update some repositories on Debian Etch installation and getting the following errors
Trying to update to a pointer from a function return. Just for background this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.