推入MongoDB的嵌套阵列

发布于 2025-02-07 03:28:32 字数 2175 浏览 0 评论 0原文

这是我的词汇模式

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

我想将一个对象插入“材料”数组中,该数组在“材料”数组中,音符数组,注释在主题内部。

我尝试了许多语法,但它们都不对我有用。最近我尝试了这个。

 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" }] 
    }}

这是完整的功能代码: -

router.get('/populate',async (req,res)=>{
    // const {data} = req.body
    // const link = "www.wiki.com"
   try {
     notes.updateOne({
        _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" }] 
    }}
        
    )
    console.log("posted") 
    const alreadyexist = await notes.find({$and:[{"year":'3'},{"sem":'2'}]})
    res.send(alreadyexist)
   // console.log(updata) 
   } catch (error) {
    console.log(error)
   }
} )

这是我当前的数据库状态。 在此处输入图像描述

This is my mongoose Schema

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

I want to insert a object into 'material' array which is inside notes array and notes is inside subjects array.

I tried many syntax, but none of them worked for me. Recently I tried this.

 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" }] 
    }}

This is full function code:-

router.get('/populate',async (req,res)=>{
    // const {data} = req.body
    // const link = "www.wiki.com"
   try {
     notes.updateOne({
        _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" }] 
    }}
        
    )
    console.log("posted") 
    const alreadyexist = await notes.find({$and:[{"year":'3'},{"sem":'2'}]})
    res.send(alreadyexist)
   // console.log(updata) 
   } catch (error) {
    console.log(error)
   }
} )

This is my current Database status.
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文