【发布时间】:2014-09-18 16:58:18
【问题描述】:
我设计了以下架构并且插入工作正常
{
"uid" : "541a5edaef7b20086c2c9ea0",
"_id" : ObjectId("541a6bca735a20060c593813"),
"exams" : [
{
"start_time" : "2014-09-18T05:21:14.219Z",
"status" : "passed",
"chapter_id" : ObjectId("54194290022f6d830f255f2e")
},
{
"start_time" : "2014-09-18T05:26:14.219Z",
"status" : "attending",
"chapter_id" : ObjectId("54194290022f6d830f255f2f")
}
],
"__v" : 0
}
如何更新 exams 键中的第二个元素,以便结果为
{
"uid" : "541a5edaef7b20086c2c9ea0",
"_id" : ObjectId("541a6bca735a20060c593813"),
"exams" : [
{
"start_time" : "2014-09-18T05:21:14.219Z",
"status" : "passed",
"chapter_id" : ObjectId("54194290022f6d830f255f2e")
},
{
"start_time" : "2014-09-18T05:26:14.219Z",
**"status" : "failed",**
"chapter_id" : ObjectId("54194290022f6d830f255f2f")
}
],
"__v" : 0
}
我的模型定义如下
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var examSchema = new Schema({
uid: String,
exams: []
});
module.exports = mongoose.model('Exam',examSchema);
我尝试了这个查询来更新,但是得到了类似的错误
Exam.findOneAndUpdate({ _id:uid, exams.chapter_id: chapterId }, { exams.status:'passed})
^
SyntaxError: Unexpected token
.
【问题讨论】: