2013-10-19 55 views
0

我試圖刪除所有嵌套的文檔不只是一個。刪除集合的所有嵌套文檔

這裏是代碼:

 //find and remove all of the embedded workorder units 
    db.workorders.find({units: { $ne: null } }, function(err, wos) { 

     console.log(wos); 
     console.log("removing the embedded workorder units"); 
     _.each(wos, function(wo) { 

     wo.units.remove(); 
     }); 

     setTimeout(function() { next() }, 2000); 

    }); 

回答

2

我想通了:

 //find and remove all of the embedded workorder units 
    db.workorders.update({}, { $set: { units: [] } }, { multi: true }, function(err) { 

     console.log("removing the embedded workorder units"); 

     next(); 

    }); 
+1

你想給我們留下一個空數組?如果你只是想刪除該字段,你可以使用{$ unset:{units:1}} –