gpt4 book ai didi

node.js - 如何插入嵌套对象数组

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:45 29 4
gpt4 key购买 nike

我有一个 mongo 文档,其中包含一个包含两个 id 数组的对象

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

我尝试过像这样推送 id

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

最佳答案

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

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

关于node.js - 如何插入嵌套对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55717686/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com