How to push into array of nested object

时间:2019-04-16 22:59:20

标签: node.js mongodb mongoose

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})

1 个答案:

答案 0 :(得分:2)

对播放列表对象使用.dot表示法

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