2

我正在尝试将“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”函数中,当我尝试将“讲师”添加到特定营地时,就会出现问题。我没有收到错误消息,而是在 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" } }
    ] 
}
4

1 回答 1

3

你可能需要看看这个。您可以使用 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 方法,例如

集合.更新集合.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" : []

            }
        ]};
于 2013-06-24T04:15:32.800 回答