推入MongoDB的嵌套阵列
这是我的词汇模式
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论