2014-07-11 26 views
0

我想删除整个集合,但它不与下面的nodejs代码一起工作。 mongodb的内同一作品使用命令[db.collection.remove()]mongoclient删除收集nodejs

  1. 没有错误,但低于返回事件的计数为0

    smevents.count(function(err, count) { 
        console.log("There are " + count + " records."); 
    }); 
    
  2. 除去的条目示出了如0从如下─

    console.log("Removed collection entries " + collectionName + "," + removed); 
    
  3. 使用的集合名是按照从所获得的一个 -

    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); 
     }); 
    }); 
} 
+0

我不知道是什么你的问题。你可以编辑你的问题,澄清你想要做什么,什么不工作?您是否尝试删除集合本身或仅删除其中的文档? – JohnnyHK

回答

0

你看起来就像所有的方法都是异步的,你需要等待他们在移动之前完成对在这里失去了回调的概念。例如,您的“计数”方法不一定在“删除”之前发生,因为您尚未等待完成。

async模块可以帮助您避免在回调中嵌套方法时出现“indentation creep”。这里是对async.waterfall逻辑的一般修改,以帮助传递所获得的变量。

注意,无论是在你的函数执行和示范流你是“移动的”下一个“任务”,当操作完成由回调里面射击:

var async = require('async'), 
    mongo = require('mongodb'); 
    MongoClient = mongo.MongoClient; 


// Function implementation 
function removeCollection(db,collectionName,callback) { 

    async.waterfall([ 

    // Get collection 
    function(cb) { 
     db.collection(collectionName,function(err,collection) { 
     if (err) throw err; 
     cb(null,collection); 
     }); 
    }, 

    // Count collection current 
    function(collection,cb) { 
     collection.count(function(err,count) { 
     console.log(
      "%s collection has %s documents", 
      collectionName, 
      count 
     ); 
     cb(null,collection); 
     }); 
    }, 

    // Remove collection documents 
    function(collection,cb) { 
     collection.remove({},function(err,removed) { 
     console.log(
      "%s collection removed %s documents", 
      collectionName, 
      removed 
     ); 
     cb(null,collection); 
     }); 
    }, 

    ],function(err,collection) { 

    collection.count(function(err,count) { 
     console.log(
     "%s collection now has %s documents", 
     collectionName, 
     count 
    ); 
     callback(err); 
    }); 

    }); 
} 

// Main flow after connection 
MongoClient.connect('mongodb://localhost/test',function(err,db) { 

    if (err) throw err; 

    async.waterfall([ 

    // Set up a collection 
    function(cb) { 
     var collectionName = "sample"; 

     db.collection(collectionName,function(err,collection) { 
     cb(null,collection,collectionName); 
     }); 
    }, 

    // Insert some things 
    function(collection,collectionName,cb) { 
     async.eachSeries([1,2,3,4,5,6,7,8],function(item,complete) { 
     collection.insert({ "item": item },function(err) { 
      if (err) throw err; 
      complete(); 
     }); 
     },function(err) { 
     // When all are complete 
     cb(null,collectionName); 
     }); 
    }, 

    // Call your remove method 
    function(collectionName,cb) { 

     removeCollection(db,collectionName,function(err) { 
     if (err) throw err; 

     cb(); 
     }); 

    } 

    ]); 

});