0

I have a document in this structure

{
  _id: 'playlistId',
  list_of_songs: [
    {
      _id: 'songId',
      song_title: 'Hello World',
      song_artists: [
        {
          artist_name: 'Aaron',
        }, {
          artist_name: 'Ben',
        }
      ]
    }
  ]
}

I'm intending to replace the entire array list_of_songs.$.song_artists for 'Hello World' in an update operation

newArrayOfArtists = [{...}, {...}, {...}]

Playlist.update({  _id: 'playlistId', }, {
  $set: {
    'list_of_songs.0.song_artists': newArrayOfArtists
  }
}

but I'm hitting this error

cannot use the part (...) to traverse the element (...)

Am I doing something wrongly, or is this due to a limitation of MongoDB (see Updating nested arrays in mongoDB via mongo shell)? I'm at a total loss here, many thanks for helping.

DongWei
  • 31
  • 5
  • You missed the quotes. `""` They are required. `"list_of_songs.0.song_artists": newArrayOfArtists`. Also see ["Dot Notation"](https://docs.mongodb.com/manual/core/document/#dot-notation) in the core documentation. – Neil Lunn Jun 02 '17 at 05:32
  • @NeilLunn Sorry, typo on my end. Quotes wasn't the problem. – DongWei Jun 02 '17 at 05:35
  • This might be minimongo specific. Is this running in the browser or server side? If browser then I think there is an issue for that. – Neil Lunn Jun 02 '17 at 05:37
  • @NeilLunn It's hitting the same issue on server and client side. – DongWei Jun 02 '17 at 05:38
  • `.update({ "_id": "playlistid" },{ "$set": { "list_of_songs.0.song_artists": [] } })` works fine for me. I just put your document in a collection and ran that statement. I suggest you have a typo somewhere. The specific error does suggest you missed the quotes though. – Neil Lunn Jun 02 '17 at 05:44
  • @NeilLunn Alright, I'll check through everything again. Thanks for the suggestions. – DongWei Jun 02 '17 at 05:45
  • Are you using simple schema or autoform? Simple schema caused exactly similar error message for me. – iiro Jun 02 '17 at 20:51
  • @iiro I've narrowed down the issue to simple-schema too, and have created an issue/workaround for it: https://github.com/aldeed/node-simple-schema/issues/136. Setting the schema to `blackbox: true` overrides the checks that causes the issue. – DongWei Jun 03 '17 at 14:01
  • Nice to know, thanks!! I had to remove defaultValue-field from the schema to get it work. – iiro Jun 03 '17 at 17:55

0 Answers0