【发布时间】:2014-07-24 16:59:41
【问题描述】:
我正在尝试删除整个集合,但它不适用于以下 nodejs 代码。使用命令 [db.collection.remove()]
在 mongodb 中同样有效-
没有错误,但以下返回事件计数为 0
smevents.count(function(err, count) { console.log("There are " + count + " records."); }); -
删除的条目从下面显示为 0-
console.log("Removed collection entries " + collectionName + "," + removed); -
使用的collectionName 是根据从-获得的一个-
db.collectionNames(function(err, collections){ console.log(collections); });
下面是代码-
function removeMongoDBCollection(db, collectionName, callback) {
console.log('collectionname ' + collectionName);
db.collectionNames(function(err, collections){
console.log(collections);
});
db.collection(collectionName, {}, function(err, smevents) {
console.log('Error Getting collection ' + err);
smevents.count(function(err, count) {
console.log("There are " + count + " records.");
});
smevents.remove({}, function(err, removed) {
console.log('Error Removing collection ' + err);
console.log("Removed collection entries " + collectionName + "," + removed);
db.close();
callback(err);
});
});
}
【问题讨论】:
-
我不确定你在问什么。您能否编辑您的问题以澄清您正在尝试做什么以及什么不起作用?您是要删除集合本身还是仅删除其中的文档?
标签: node.js mongodb node-mongodb-native