2017-03-03 33 views
-1

我不得不ArrayList中特定文檔的要求MongoDB中的陣列的文檔時,用於該樣品JSON字符串是更新在使用webMethods

{ 「_id」:的ObjectId(「58b9339502be203f6b476664」), 「_docType」: 「測試」, 「類型」: 「mongoUpdate」,

"createdDateTime" : "2017-03-03 09:12:53.080", 
"contacts" : [ 
    { 

     "firstName" : "FirstName", 
     "lastName" : "LastName", 
     "email" : "[email protected]", 
     "contactType" : "Business.", 
     "phoneNumber" : "1234567890",   
     "createdDateTime" : "2017-03-03 09:13:04.229",    
     "lastModifiedDTM" : "2017-03-03 09:13:04.229", 
     }, 
    { 

     "firstName" : "FirstName2", 
     "lastName" : "LastName2", 
     "email" : "[email protected]", 
     "contactType" : "Business2.", 
     "phoneNumber" : "1234567890",   
     "createdDateTime" : "2017-03-03 09:13:04.229",    
     "lastModifiedDTM" : "2017-03-03 09:13:04.229", 
     }, 
    { 

     "firstName" : "FirstName3", 
     "lastName" : "LastName3", 
     "email" : "[email protected]", 
     "contactType" : "Business.3", 
     "phoneNumber" : "12345678903",   
     "createdDateTime" : "2017-03-03 09:13:04.229",    
     "lastModifiedDTM" : "2017-03-03 09:13:04.229", 
     } 
] 

}

說我有更新在上述JSON發生中的一個。 我已經使用$ set操作來更新上面的數組,並且當我看到mongo數據庫中的內容時,我看到整個數組被替換爲單次發生的聯繫。

我所使用的更新命令是

{$組:{ 「接觸」:[{ 「名字」: 「test0103)12」, 「姓氏」: 「test0103」, 「電子郵件」:」 test0103「,」contactType「:」test0103「,」phoneNumber「:」test0103「,」createdDateTime「:」test0103「}]}}

執行此操作後,我看到整個數組列表3被替換爲單個聯繫人實例。

並在結束我有輸出作爲 「接觸」:[{

"firstName" : "test0103)12", 
    "lastName" : "test0103", 
    "email" : "test0103", 
    "contactType" : "test0103", 
    "phoneNumber" : "test0103",   
    "createdDateTime" : "test0103"   

    }] 
+0

那是因爲你不要像你在做的那樣更新對象的數組。數組內對象的操作在貓鼬中完成的方式不同,並且有許多與它有關的問題,您可以發現它們。 –

回答

0

下面是示例代碼

首先找到使用_id該文檔。這裏收集了我的用戶

` User.findOne({'_id': 58b9339502be203f6b476664},(err,res) => { 
     if(res.contacts.length > 0){ 
      var contacts = res.contacts; 
      var contactsList = []; 
      var contactobject = {}; 
     // suppose you want to update the contact with firstname - FirstName2 
      contacts.map((contact,key)=>{ 
        if(contact.firstName == "firstName2"{ 

         contactobject.firstName = "Your updated name"; 
         contactobject.lastName = "Your updated name"; 
         // similary all values you want to update 
         // Then push to an array after updating new data 
         contactsList.push(contactObject); 

        } 
       else { 
        // if not the required contact to update 
        // simply push 
        contactsList.push(contact); 

       } 

      }) 

//地圖功能後,立即更新了新的聯繫人

 User.update({email:email}, 
          {$set:{contacts: contactsList}},(err,result) => { 
             User.findOne({'_id':res._id}).then(updateuser => { 
           resolve(updateuser); 
             }) 
            }) 
     } 

})`

試試這個,它的工作原理

+0

感謝您的回覆,但這並不能幫助我,因爲我需要webMethods代碼。所以我做了相應的改變和工作。 – abhijith501

+0

感謝您的迴應,但這並不能幫助我,因爲我需要webMethods代碼。 所以我做了相應的變化和工作。 我已先取從蒙戈的聯繫人,然後環繞在從蒙戈 檢索結果,並環繞在我的輸入列表 如果他們是平等的,我忽略了他們,如果他們不相等,那麼我形成一個查詢 說例如「contacts。$。firstName」:「inputValue」,對於列表中的所有值也是如此。 然後,我得到整個列表後,我正在做一個$集,將取代新的值。 – abhijith501