2014-03-26 23 views
13

我有一個集合aTable有2個記錄:更新在foreach所MongoDB的外殼

{ 
    "title" : "record 1", 
    "fields" : [ 
     { 
      "_id" : 1, 
      "items" : [ 
       1 
      ] 
     }, 
     { 
      "_id" : 2, 
      "items" : [ 
       2,3,4 
      ] 
     }, 
     { 
      "_id" : 3, 
      "items" : [ 
       5 
      ] 
     } 
    ] 
}, 

{ 
     "title" : "record 2", 
     "fields" : [ 
      { 
       "_id" : 4, 
       "items" : [ 
        7,8,9,10 
       ] 
      }, 
      { 
       "_id" : 5, 
       "items" : [ 

       ] 
      }, 
      { 
       "_id" : 6, 
       "items" : [ 
        11,12 
       ] 
      } 
     ] 
    } 

我想從

items" : [ 11,12 ] 

更新領域 aTable.fields.items
items" : [ 
    {item: 11, key: 0}, 
    {item:12, key: 0} 
] 

i browse fields與forEach但我無法保存它。

var t = db.aTable.find(); 

t.forEach(function(aRow) { 
    aRow.fields.forEach(function(aField){ 
     aField.items.forEach(function(item){ 
      var aNewItem = { item: parseInt(item), ref: 0 }; 
      db.aTable.update(item, {$set:aNewItem}) 
     }) 
    }) 
}); 

請幫幫我。非常感謝您

+0

是否有任何錯誤?另一個選擇是直接在aRow對象中進行更改,然後調用db.aTable.save(aRow) – Sikorski

+0

,因爲** aRow.fields **是數組,** aRow.fields **中的每個元素都有多個項目* *。 **項目**是數組。所以我們不能直接更新** aRow ** –

回答

15

爲了得到你想要的,你需要的幾件事情是什麼:

t.forEach(function(aRow) { 
    var newFields = []; 
    aRow.fields.forEach(function(aField){ 
     var newItems = []; 
     aField.items.forEach(function(item){ 
      var aNewItem = { item: parseInt(item), ref: 0 }; 
      newItems.push(aNewItem); 
     }); 
     newFields.push({ _id: aField._id, items: newItems }); 
    }) 
    aTable.update(
     { _id: aRow._id }, 
     { "$set": { "fields": newFields } } 
    ); 
}); 

所以基本上你需要之前「重新構建」你的陣列更新

9

您可以直接在整個對象中進行更改並保存。試試下面的代碼片段

db.aTable.find().forEach(function (itemWrapper){ 
    itemWrapper.fields.forEach(function(field){ 
     var items = field.items; 
     var newItems = []; 
     items.forEach(function(item){ 
      var t = {'item':item,'key':0} 
      newItems.push(t);  
     }) 
     field.items = newItems; 
    }) 
    db.aTable.save(itemWrapper) 
}) 

什麼我做的是遍歷所有項目並且與{item : 1 , key:0}一個新的數組,然後在現場對象設置回項目陣列。

這是更新後的輸出:

{ 
    "_id" : ObjectId("5332a192ece4ce8362c7a553"), 
    "title" : "record 1", 
    "fields" : [ 
     { 
      "_id" : 1, 
      "items" : [ 
       { 
        "item" : 1, 
        "key" : 0 
       } 
      ] 
     }, 
     { 
      "_id" : 2, 
      "items" : [ 
       { 
        "item" : 2, 
        "key" : 0 
       }, 
       { 
        "item" : 3, 
        "key" : 0 
       }, 
       { 
        "item" : 4, 
        "key" : 0 
       } 
      ] 
     }, 
     { 
      "_id" : 3, 
      "items" : [ 
       { 
        "item" : 5, 
        "key" : 0 
       } 
      ] 
     } 
    ] 
} 

/* 1 */ 
{ 
    "_id" : ObjectId("5332a192ece4ce8362c7a554"), 
    "title" : "record 2", 
    "fields" : [ 
     { 
      "_id" : 4, 
      "items" : [ 
       { 
        "item" : 7, 
        "key" : 0 
       }, 
       { 
        "item" : 8, 
        "key" : 0 
       }, 
       { 
        "item" : 9, 
        "key" : 0 
       }, 
       { 
        "item" : 10, 
        "key" : 0 
       } 
      ] 
     }, 
     { 
      "_id" : 5, 
      "items" : [] 
     }, 
     { 
      "_id" : 6, 
      "items" : [ 
       { 
        "item" : 11, 
        "key" : 0 
       }, 
       { 
        "item" : 12, 
        "key" : 0 
       } 
      ] 
     } 
    ] 
}