2015-06-05 28 views
0

我想遠程項目只包含空格" "我怎麼能刪除2級深度嵌套數組的一些項目

但我的查詢不起作用,任何建議

查詢

db[source_collection].update({ 
    '_id': customer._id 
    }, { 
    '$pull': { 
     "records.0.items": { 
     $regex: /^\s*$/ 
     } 
    } 
    }); 

文獻
{ 
     "_id" : "025daee4b9b9ba36ed244bb594313ff9", 
     "records" : [ 
      { 
       "items" : [ 
        "7089 " 
       ] 
      }, 
      { 

       "items" : [ 
        "  ", 
        "78059" 
       ] 
      }, 
      { 

       "items" : [ 
        "  ", 
        "5645 " 
       ] 
      } 
     ] 
    } 
+0

嗨@yogesh我會稍後再嘗試,非常感謝。它需要我有一段時間來弄清楚。 – newBike

回答

0
db[source_collection].find({"_id":customer._id}).forEach(function(doc){ 
var arr = doc.records.items; 
for(var i; i<arr.length;i++) 
{ 
    if(!arr[i].replace(/\s/g, '').length) 
    arr.splice(i,1); 
} 
doc.records.items = arr; 
db[source_collection].save(doc); 
}