nodejs/mongodb,删除整个集合

Pau*_*ulM 7 mongodb node.js

我正在尝试删除集合中的所有项目.

db.collection('sessions', function(err, collection) {                                      
    collection.remove();                 
});
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

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)
Run Code Online (Sandbox Code Playgroud)

但是,我可以通过mongodb罚款这样做:

db.sessions.remove();
Run Code Online (Sandbox Code Playgroud)

通过节点实现我想要的最好方法是什么?

谢谢

Pau*_*ulM 14

回到这个......只是为了更新问题.

store.collection('sessions',function(err, collection){
    collection.remove({},function(err, removed){
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 什么是"商店"? (2认同)

The*_*ith 8

我知道这对派对来说有点晚了,而且已经发生了很多变化,但是要删除 node 中的集合,您可以这样做:

db.collection('someCollection').drop();
Run Code Online (Sandbox Code Playgroud)

从 mongo 终端你这样做:

db.someCollection.drop();
Run Code Online (Sandbox Code Playgroud)

就这么简单。