UnhandledPromiseRejectionWarning: MongoError: Cannot create field 'semesters' in element
UnhandledPromiseRejectionWarning: MongoError: Cannot create field 'semesters' in element
MongoDB 中的数据库:
{
"_id" : ObjectId("610144532aa8eb54745f7501"),
"courses" : [
{
"_id" : ObjectId("61014585d71b225516d0116f"),
"course" : "MBA",
"semesters" : [
{
"_id" : ObjectId("61014587d71b225516d01174"),
"sem" : 1,
"subjects" : [ ]
}
]
}
],
"__v" : 0
}
架构:
const Subject = mongoose.Schema({
subject : String,
units : [UnitSchema]
});
我用来在主题数组中添加文档的代码
app.get('/insertSubject', async function(req, res)
{
const subject = new Subject({
subject : req.body.subject
})
await Padhlo.updateOne(
{"courses.course":req.body.course , "courses.semesters.sem":parseInt(req.body.semester)},
{$addToSet : {"courses.semesters.$.subjects":subject}},
function(err)
{
if(!err)
{
res.status(200).send("OK");
}
else
{
res.status(404).send(err);
}
}
)
});
错误:
UnhandledPromiseRejectionWarning: MongoError: Cannot create field 'semesters' in element
我想在 subjects 数组中添加一个文档,但出现此错误,更新后的数据库应该如下所示:
{
"_id" : ObjectId("610144532aa8eb54745f7501"),
"courses" : [
{
"_id" : ObjectId("61014585d71b225516d0116f"),
"course" : "MBA",
"semesters" : [
{
"_id" : ObjectId("61014587d71b225516d01174"),
"sem" : 1,
"subjects" : [
subject : "BCA",//added new field according to Schema
units : [ ]
]
}
]
}
],
"__v" : 0
}
请帮助我并告诉我我哪里出错了我是新手,对 mongoDB 我使用的版本 MongoDB : 5.0.1
可以参考MongoDB The all positional operator
The all positional operator $[] indicates that the update operator should modify all elements in the specified array field.
试试这个查询
await Padhlo.updateOne(
{ "courses.course": req.body.course , "courses.semesters.sem": parseInt(req.body.semester) },
{ "$addToSet" : { "courses.$[].semesters.$[].subjects": subject } }
)
MongoDB 中的数据库:
{
"_id" : ObjectId("610144532aa8eb54745f7501"),
"courses" : [
{
"_id" : ObjectId("61014585d71b225516d0116f"),
"course" : "MBA",
"semesters" : [
{
"_id" : ObjectId("61014587d71b225516d01174"),
"sem" : 1,
"subjects" : [ ]
}
]
}
],
"__v" : 0
}
架构:
const Subject = mongoose.Schema({
subject : String,
units : [UnitSchema]
});
我用来在主题数组中添加文档的代码
app.get('/insertSubject', async function(req, res)
{
const subject = new Subject({
subject : req.body.subject
})
await Padhlo.updateOne(
{"courses.course":req.body.course , "courses.semesters.sem":parseInt(req.body.semester)},
{$addToSet : {"courses.semesters.$.subjects":subject}},
function(err)
{
if(!err)
{
res.status(200).send("OK");
}
else
{
res.status(404).send(err);
}
}
)
});
错误:
UnhandledPromiseRejectionWarning: MongoError: Cannot create field 'semesters' in element
我想在 subjects 数组中添加一个文档,但出现此错误,更新后的数据库应该如下所示:
{
"_id" : ObjectId("610144532aa8eb54745f7501"),
"courses" : [
{
"_id" : ObjectId("61014585d71b225516d0116f"),
"course" : "MBA",
"semesters" : [
{
"_id" : ObjectId("61014587d71b225516d01174"),
"sem" : 1,
"subjects" : [
subject : "BCA",//added new field according to Schema
units : [ ]
]
}
]
}
],
"__v" : 0
}
请帮助我并告诉我我哪里出错了我是新手,对 mongoDB 我使用的版本 MongoDB : 5.0.1
可以参考MongoDB The all positional operator
The all positional operator $[] indicates that the update operator should modify all elements in the specified array field.
试试这个查询
await Padhlo.updateOne(
{ "courses.course": req.body.course , "courses.semesters.sem": parseInt(req.body.semester) },
{ "$addToSet" : { "courses.$[].semesters.$[].subjects": subject } }
)