我对 mongodb 非常陌生,并且遇到了一个基本问题。如何获取已创建文档的 ID 字段?我需要 ID,以便可以更新/添加新字段到文档。
//newProfile is an object, one string it holds is called school
if(Schools.find({name: newProfile.school}).fetch().length != 1){
var school = {
name: newProfile.school
}
Meteor.call('newSchool', school);
//Method 1 (doesn't work)
var schoolDoc = Schools.findOne({name: newProfile.school});
Schools.update({_id: schoolDoc._id}, {$set: {enrolledStudents: Meteor.user()}});
//Method 2?
//Schools.update(_id: <what goes here?>, {$push: {enrolledStudents: Meteor.user()}});
}
else {
//Schools.update... <add users to an existing school>
}
如果列出的学校尚不存在,我将创建一个新的学校文档。学校需要保存一个学生数组/列表(这是我遇到麻烦的地方)。如何将学生添加到新字段(称为注册学生)?
谢谢!