我正在尝试将“教师”数组添加到已有的“Camps”数组中。
层次结构看起来像这样:
owner = {
email : 'john.smith@gmail.com',
password : 'mypassword',
firstName : 'john',
lastName : 'smith',
camps : [
{
name : 'cubs-killeen',
location : 'killeen',
manager : {name: 'joe black', email: '', password: ''},
instructors : [
{
firstName : 'bill',
lastName : 'jones',
classes : []
},
{
firstName : 'jill',
lastName : 'jones',
classes : [],
},
],
students : []
}
]
};
我正在使用Node Express和MongoJS并且能够成功添加所有者并添加“阵营”,但是,在“addInstructor”函数中,当我尝试将“教师”添加到特定阵营时出现问题。我没有得到任何错误消息,而是简单地在阵营阵列中的项目之后附加“教师”数组。
非常感谢任何帮助。下面是我的完整代码,有工作函数,然后是一个不工作的,下面是我的mongodb输出(虽然错误):
CampRepository = function(){};
CampRepository.prototype.addOwner = function(owner, callback){
console.log(db);
db.owners.save(owner, function(err, saved){
if (err || !saved) {
console.log('broke trying to add owner : ' + err);
callback(err);
} else {
console.log('save was successful');
callback(null, saved);
}
});
};
CampRepository.prototype.addCamp = function(ownerEmail, camp, callback){
db.owners.update(
{email: ownerEmail},
{$push: {
camps:{
name: camp.name,
location: camp.location,
managerName: camp.managerName,
managerEmail: camp.managerEmail,
managerPassword: camp.managerPassword,
managerPayRate: camp.managerPayRate,
instructors: [],
students: []
}
}
}, function(err, saved){
if (err || !saved) {
console.log('broke trying to add camp ' + err);
callback(err);
} else {
console.log('save was successful');
callback(null, saved);
}
});
};
/*
THIS IS THE ONE THAT DOESN'T WORK
*/
CampRepository.prototype.addInstructor = function(ownerEmail, campName, instructor, callback){
db.owners.update(
{email: ownerEmail, 'camps.name': campName},
{$push: {
camps:{
instructors: {
firstName: instructor.firstName,
lastName: instructor.lastName,
email: instructor.email
},
}
}
}, function(err, saved){
if (err || !saved) {
console.log('broke trying to add camp ' + err);
callback(err);
} else {
console.log('save was successful');
callback(null, saved);
}
});
};
输出
{
"_id" : ObjectId("51c7b04d2746ef6078000001"),
"email" : "john.smith@gmail.com",
"firstName" : john,
"lastName" : smith,
"password" : "mypassword",
"camps" : [
{
"name" : "cubs-killeen",
"location" : "killeen",
"managerName" : "bill jones",
"managerEmail" : "bill@gmail.com",
"managerPassword" : "secretpasscode",
"instructors" : [ ],
"students" : [ ]
},
{ "instructors" : { "name" : "jon tisdale" } }
]
}
答案 0 :(得分:2)
您可能需要查看this。你可以使用dot.notation实现这一点 这是在更大的文档方案中查找或更新项目的非常强大的方法。如果您仍然无法实现这一点,我很乐意为您提供以下代码......
我已插入新的拥有者2
owner2 = {
email : 'murali.ramakrishnan@gmail.com',
password : 'mypassword',
firstName : 'murali',
lastName : 'ramakrishnan',
camps : [
{
name : 'Rotary club',
location : 'trichy',
manager : {name: 'baskaran', email: 'baskaran@mit.edu', password: 'baskaran'},
instructors : [
{
firstName : 'baskaran',
lastName : 'subbiah',
classes : []
},
{
firstName : 'david',
lastName : 'nover',
classes : [],
},
],
students : []
}
]};
如果您发现我们只需要按要求添加新讲师...... 首先让文档添加到集合
db.try.insert(owner2);
在这里你添加了一个新文档 现在,我将创建一个新的教师对象来插入@newly created owner2
instructor1 = {
firstName : 'lakshmi',
lastName : 'kanthan',
classes : []
};
上面的是新教师的文档对象 您可以使用mongodbs方法(如
)以多种方式执行此更新collection.update collection.findAndModify
如果要插入或更新我们需要使用dot.notation查找的子文档的任何值,并将子文档推送到文档,这里是代码
db.try.update(
{'camps.name': "Rotary club" },
{
$push: { 'camps.$.instructors' : instructor1 }
}
)
上面的代码在教师字段下插入一条新记录,就像在字段中一样,它只是推送子文档
最终结果
{
"_id" : ObjectId("51c7b222c0468dc711a60916"),
"email" : "murali.ramakrishnan@gmail.com",
"password" : "mypassword",
"firstName" : "murali",
"lastName" : "ramakrishnan",
"camps" : [
{
"name" : "Rotary club",
"location" : "trichy",
"manager" : {"name": "baskaran", "email": "baskaran@mit.edu", "password": "baskaran"},
"instructors" : [
{
"firstName" : "baskaran",
"lastName" : "subbiah",
"classes" : []
},
{
"firstName" : "david",
"lastName" : "nover",
"classes" : [],
},
{
"firstName" : "lakshmi",
"lastName" : "kanthan",
"classes" : [],
}
],
"students" : []
}
]};