[英]How to update MongoDB documents with arrays of sub-documents
I am working with a MongoDB database whose collections model classes , students , subjects , and [academic] performance . 我正在使用一个MongoDB数据库,该数据库的收集模型包括班级 , 学生 , 学科和[学术] 表现 。 Below are the Mongoose based schema and models:
以下是基于猫鼬的架构和模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
// SUBJECT collection
var subjectSchema = new Schema({
name : { type: String, required: true },
category : { type: String, required: true }
});
var Subject = mongoose.model('Subject', subjectSchema);
// STUDENT collection
var studentSchema = new Schema({
name : { type: String, required: true },
grade : { type: Number, required: true }
});
var Student = mongoose.model('Student', studentSchema);
// PERFORMANCE collection
var performanceSchema = new Schema({
subjectId : { type: ObjectId, ref: Subject.modelName, required: true },
score : { type: Number, required: true },
maximum : { type: Number, required: true },
grade : { type: String, required: true }
});
var Performance = mongoose.model('Performance', performanceSchema);
// *Note*: This is just to use as a sub-document schema, and not as a collection
var classStudentSchema = new Schema({
studentId : { type: ObjectId, ref: Student.modelName, required: true },
performance : [performanceSchema]
}, { _id: false });
// CLASS collection
var classSchema = new Schema({
name : { type: String, required: true },
scores : [classStudentSchema]
});
var Class = mongoose.model('Class', classSchema);
The class
collection's documents are the most complex of the lot; class
收藏的文件是最复杂的。 an example document would be: 示例文档为:
{
"_id" : ObjectId("57758f15f68da08c254ebee1"),
"name" : "Grade 5 - Section A",
"scores" : [{
"studentId" : ObjectId("5776bd36ffc8227405d364d2"),
"performance": [{
"subjectId" : ObjectId("577694ecbf6f3a781759c54a"),
"score" : 86,
"maximum" : 100,
"grade" : "B+"
}, {
"subjectId" : ObjectId("5776ffe1804540e29c602a62"),
"score" : 74,
"maximum" : 100,
"grade" : "A-"
}]
}, {
"studentId" : ObjectId("5776bd36ffc8227405d364d5"),
"performance": [{
"subjectId" : ObjectId("577694ecbf6f3a781759c54a"),
"score" : 94,
"maximum" : 100,
"grade" : "A"
}, {
"subjectId" : ObjectId("5776ffe1804540e29c602a62"),
"score" : 81,
"maximum" : 100,
"grade" : "A"
}]
}]
}
I was able to retrieve an existing class document and add a student to it's scores using the following code: 我能够使用以下代码检索现有的班级文档并将其添加到分数中:
Class.findOne({ name: 'Grade 5 - Section A' }, function(err, class) {
if (err) throw err;
Student.findOne({ name: 'John Doe' }, function(err, student) {
if (err) throw err;
class.scores.push({
studentId: student.id
});
};
});
But how do I add/update/delete that particular student's performance? 但是,如何添加/更新/删除该特定学生的成绩? I need to be able to interact with the
class
collection in the following ways: 我需要能够通过以下方式与
class
集合进行交互:
scores
array) scores
(检索scores
数组中的特定元素) scores[n].performance
array; for add, append to the same array. scores[n].performance
数组中检索特定元素;对于添加或添加到同一数组。 There are a few way to do this I'll answer point by point 有几种方法可以逐步解决
Retrieve the scores for all students, or for a specific student (retrieve specific element in the scores array) 检索所有学生或特定学生的分数(检索分数数组中的特定元素)
Class.findOne({ name: 'Grade 5 - Section A'})
.populate('scores.studentId')
.exec(function(err, class) {
if (err) throw err;
//now class.scores.studentId becomes ObjectStudent
//hence you have all scores for all students
});
Add/ update/ delete a specific student's score, for a specific subject (in case of update or delete, retrieve a specific element in the scores[n].performance array; for add, append to the same array. 为特定科目添加/更新/删除特定学生的分数(如果更新或删除,则在scores [n] .performance数组中检索特定元素;对于添加或添加到同一数组。
Class.findOneAndUpdate({name: 'Grade 5 - Section A'
,'scores.studentId': ObjectId('5776bd36ffc8227405d364d2')
, 'scores.performance.subjectId' : ObjectId('577694ecbf6f3a781759c54a')}
, {$set: {scores.performance. score: 50}}
, function(err, data) {
if (err) throw err
});
I hope that helps 希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.