gpt4 book ai didi

javascript - MongoDB:将数组添加到现有数组中

转载 作者:可可西里 更新时间:2023-11-01 10:33:11 26 4
gpt4 key购买 nike

我正在尝试将一个“Instructors”数组添加到一个已经存在的“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”函数中,当我尝试将“Instructors”添加到特定营地时出现问题。我没有收到任何错误消息,而是在 camps 数组中的项目之后简单地附加了“Instructors”数组。

如有任何帮助,我们将不胜感激。下面是我的完整代码,有工作函数,然后是不工作的代码,下面是我的 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" } }
]
}

最佳答案

您可能需要看一下 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);

到这里你已经添加了一个新文档现在,我要创建一个新的 instructor 对象来插入@newly created owner2

instructor1 = {
firstName : 'lakshmi',
lastName : 'kanthan',
classes : []
};

上面是新讲师的文档对象您可以通过多种方式执行此更新,使用像

这样的 mongodbs 方法

集合.更新collection.findAndModify

如果您想向子文档插入或更新任何值,我们需要使用点符号查找并将子文档推送到文档,这里是代码

db.try.update(
{'camps.name': "Rotary club" },
{
$push: { 'camps.$.instructors' : instructor1 }
}
)

上面的代码在 instructor 字段下插入一条新记录,因为在该字段中它只是推送子文档的数组

最终结果

{
"_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" : []

}
]};

关于javascript - MongoDB:将数组添加到现有数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17267213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com