2015-11-20 24 views
0

我碰到MongoDB的所有集合的列表中刪除一個特定的項目,以形成一個陣列的的NodeJS - 從數組

mongoose.connection.db.listCollections().toArray(function (err, names) { 
    if (err) { 
     console.log(err); 
    } 
    console.log(names); 

輸出:

我想刪除system.indexes來自陣列。我試着用一些功能玩耍,如:

  • 拼接
  • 流行,移
  • 下劃線的without功能

好吧,老實說,我甚至不知道,如果他們打算爲這一點。

+0

你想刪除整個對象{名稱:「system.indexes」}或名稱的只是財產? – Bernhard

+0

@伯恩哈德整個對象。所以輸出的結果是'[{name:'books'},...]' – xperator

回答

0

要刪除其中名稱等於system.indexes做如下的對象:

mongoose.connection.db.listCollections().toArray(function (err, names) { 
    if (err) { 
     console.log(err); 
    }else{ 
    var i; 

    for(i=names.length - 1; i >= 0; i-=1){ 
     if(names[i].name !== 'system.indexes'){ 
      names.splice(i,1); 
     } 
    } 

    // names now contain all items without the system.indexes 
    } 
+0

那麼,它確實有效。但我很困惑。我們真的必須這樣做嗎?我的意思是使用循環和重新創建數組是有點..塑造。 – xperator

+0

@xperator你說得很對,但最好是向後迭代。更新了我的答案 – Bernhard