2013-06-24 60 views
2

我試圖將「Instructors」數組添加到已存在的「Camps」數組中。MongoDB:將數組添加到現有陣列

層次結構看起來是這樣的:

owner = { 

    email : '[email protected]', 
    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 : [] 

       } 
      ] 
}; 

我使用節點快遞與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" : "[email protected]", 
    "firstName" : john, 
    "lastName" : smith, 
    "password" : "mypassword", 
    "camps" : [ 
       { 
        "name" : "cubs-killeen",  
        "location" : "killeen",  
        "managerName" : "bill jones",  
        "managerEmail" : "[email protected]", 
        "managerPassword" : "secretpasscode",  
        "instructors" : [ ],  
        "students" : [ ] 
       },  
       { "instructors" : { "name" : "jon tisdale" } } 
    ] 
} 
+1

陣列需要使用的位置操作該字段。更新({...},{$ push:{「camps。$。instructors」:etc}}) –

+0

感謝您的反饋!對此,我真的非常感激!我將其他答案標記爲正確的,以便他在答案中付出的巨大努力,儘管在技術上你們都是正確的。再次感謝! :-) – cpeele00

回答

2

您可能需要採取看看this。你可以使用dot.notation來實現這個 這是一個非常強大的方式來查找或更新大型文檔方案中的項目。如果你仍然無法實現這一目標,我將竭誠爲您提供以下代碼...

我插入一個新的owner2

owner2 = { 

email : '[email protected]', 
password : 'mypassword', 
firstName : 'murali', 
lastName : 'ramakrishnan', 

camps : [ 

      { 

       name : 'Rotary club', 
       location : 'trichy', 
       manager : {name: 'baskaran', email: '[email protected]', password: 'baskaran'}, 

       instructors : [ 

        { 

         firstName : 'baskaran', 
         lastName : 'subbiah', 

         classes : []       
        }, 

        { 

         firstName : 'david', 
         lastName : 'nover', 

         classes : [], 

        }, 

       ], 

       students : [] 

      } 
     ]}; 

如果你看到我們只需要添加一個新的教練爲請... 讓第一文檔添加到集合

db.try.insert(owner2); 

在這裏你去你已經添加了一個新的文檔 現在,我將創建一個新的教練的對象要插入@newly owner2

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

以上是對新教練的文檔對象 你可以在很多方面像

collection.update如果你想插入或更新執行此更新,使用mongodbs方法 collection.findAndModify

我們需要使用dot.notation找到子文檔的任何值,並將子文檔推送到文檔中,此處的代碼爲

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

上述代碼插入指導員場下一個新的記錄,如它只是推動子文檔

歸宿

{ 
"_id" : ObjectId("51c7b222c0468dc711a60916"), 
"email" : "[email protected]", 
"password" : "mypassword", 
"firstName" : "murali", 
"lastName" : "ramakrishnan", 

"camps" : [ 

      { 

       "name" : "Rotary club", 
       "location" : "trichy", 
       "manager" : {"name": "baskaran", "email": "[email protected]", "password": "baskaran"}, 

       "instructors" : [ 

        { 

         "firstName" : "baskaran", 
         "lastName" : "subbiah", 

         "classes" : []       
        }, 

        { 

         "firstName" : "david", 
         "lastName" : "nover", 

         "classes" : [], 

        }, 
     { 

         "firstName" : "lakshmi", 
         "lastName" : "kanthan", 

         "classes" : [], 

        } 

       ], 

       "students" : [] 

      } 
     ]}; 
+0

它的工作!非常感謝您花時間解釋它!對此,我真的非常感激! – cpeele00

+0

所以我試圖應用這個相同的邏輯來添加一個項目到類的數組,但它不工作。有任何想法嗎? db.owners。更新( { 電子郵件: 「[email protected]」, 'camps.name': 「崽 - 基林」, 'instructors.lastName': 「Tisdale的」 },{ $ 推:{ '。camps。$。instructors。$。classes':{type:「painting」}} } ) – cpeele00

+1

您只能有一個位置運算符:https://jira.mongodb.org/browse/SERVER-831 –