2011-11-07 52 views
4

我試圖刪除集合中的所有項目。nodejs/mongodb,刪除整個集合

db.collection('sessions', function(err, collection) {          
    collection.remove();     
}); 

這是錯誤我得到:

node.js:134 
     throw e; // process.nextTick error, or 'error' event on first tick 
     ^
TypeError: Cannot call method 'getRequestId' of null 
    at [object Object].executeCommand (/srv/www/www.cidev.com/nodejs/node_modules/mongodb/lib/mongodb/db.js:778:48) 
    at Collection.remove (/srv/www/www.cidev.com/nodejs/node_modules/mongodb/lib/mongodb/collection.js:199:26) 
    at /srv/www/www.cidev.com/nodejs/session/memory/index.js:15:20 
    at [object Object].collection (/srv/www/www.cidev.com/nodejs/node_modules/mongodb/lib/mongodb/db.js:197:12) 
    at new <anonymous> (/srv/www/www.cidev.com/nodejs/session/memory/index.js:14:11) 
    at Object.<anonymous> (/srv/www/www.cidev.com/nodejs/session/memory/index.js:157:16) 
    at Module._compile (module.js:411:26) 
    at Object..js (module.js:417:10) 
    at Module.load (module.js:343:31) 
    at Function._load (module.js:302:12) 

不過,我可以做到這一點通過MongoDB的罰款:

db.sessions.remove(); 

什麼來實現我通過節點想要什麼是最好的辦法嗎?

感謝

回答

13

讓我們再回到這個......只是爲了更新問題。

store.collection('sessions',function(err, collection){ 
    collection.remove({},function(err, removed){ 
    }); 
}); 
+2

什麼是'store'? – fstab

+0

商店是數據庫對象 – dyesdyes

0

我不確定你是否能夠弄清楚這一點,但你的代碼不適合我...也許是因爲API的變化?反正我可以用下面的代碼刪除整個集合的內容:

db.CollectionName.remove().exec(function(error) { 
    if(error) { 
     console.log('Uh oh: ' + error); 
    } 
    else { 
     console.log(' [Existing Collection Deleted]'); 
    } 
}); 
2

我知道這是遲到了一點,太大改變,但remove a collection in node,你這樣做:

db.collection('someCollection').drop(); 

從蒙戈終端你這樣做:

db.someCollection.drop(); 

就這麼簡單。