【问题标题】:Pushing into Nested array in mongodb推入 mongodb 中的嵌套数组
【发布时间】:2022-06-14 14:17:51
【问题描述】:

这是我的猫鼬模式

const notesSchema = mongoose.Schema({
    college: {type:String},
    year: {type:String},
    sem: {type:String},
    branch:{type:String},
    subjects: [
        {
            subject_id:{type:String},
            name:{type:String},
            codename: {type:String},
            notes: [
                {
                    notes_id:{type:String}, 
                    title: {type:String},
                    material: [
                        {
                            material_id:{type:String},
                            heading:{type:String},
                            link: {type:String}
                        }
                    ]
                }
            ]
        }
    ]
})

我想将一个对象插入到notes数组内的“material”数组中,而notes在subjects数组内。

我尝试了很多语法,但没有一个适合我。最近我试过这个。

 try {
     notes.updateOne({
        $and:[ {_id: '62a61949dc0f920ae99fc687'}, {'subjects.notes.notes_id':'221fad-f35c-ee2a-65b3-8531dbfcf732'}] 
    }, 
        {$push:{'subjects.0.notes.$.material': 
        [{ material_id: "hfklahfhoabfoab", heading: "Prime", link: "wwo.prime.com" }] 
    }}

【问题讨论】:

  • 你是什么意思'没有工作?'他们是否更新但清除了其他所有内容?我的猜测(已经有一段时间了)是您需要设置 merge 属性以确保 old 数据与新更改合并。这只是我的想法,但我们需要更多信息来帮助你。

标签: javascript node.js mongodb mongodb-query mongoose-schema


猜你喜欢
  • 1970-01-01
  • 2023-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 2020-03-27
相关资源
最近更新 更多