Jeremy Nelson
Jeremy Nelson

Reputation: 286

How to push into array of nested object

I have a mongo doc with an object that holds two arrays of id's

Musics {
_id: ""evsdfbsfb"
name: "muh jams"
playlists: {
 type1: ["dfngdfgnd", "dfgdfgn"]
 type2: ['dfgndfndgn','dndgndgndgnd"]
 }
}

I've tried pushing an id like so

let music = await MusicModel.findOneAndUpdate({_id:input.music}, { playlists: { "$push": { "type1" : type1._id }}}, {new: true})

Upvotes: 1

Views: 44

Answers (1)

Ashh
Ashh

Reputation: 46441

Use .dot notation with playlist object

await MusicModel.findOneAndUpdate(
  { "_id":  input.music },
  { "playlists.type1": { "$push": type1._id }},
  { "new": true }
)

Upvotes: 2

Related Questions