简体   繁体   中英

Remove element in array by its index - mongoose

I have a mongoose model that looks like this:

 Member: {
  memberId: {
    type: String,
    required: true
  },
  customPlaylists: {
    type: Array,
    default: []
  }
       
         }

customPlaylist's array holds object that look like this:

{ name: 'playlist name', urls: [] } // urls array contains url strings

Is there a way using mongoose's updateOne method to remove a url from the urls array by its index in the array? (I use the member's ID in the filter object)

I'm not sure if there is a straight (1 query) way to do this. But you can try this

<collectionName>.update({}, {$unset : {"urls.<indexNumberHere>" : 1 }}) 
<collectionName>.update({}, {$pull : {"urls" : null}})

There was a ticket to implement this feature and the resolution was Won't Do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM