集合没有在mongodb中删除

时间:2013-03-20 09:38:00

标签: node.js mongodb cron mongoose

嗨我是performin根据时间删除我的数据库。我使用cron设置时间并删除db.Below是代码

var cronJob = require('cron').CronJob;
new cronJob('* * * * *', function(){// set the required time
  console.log('inside cron');
  console.log(new Date());
  console.log('You will see this message every minute');
  dbDatadeletion();
}, null, true);

//This function performs the required operations after a particular time
function dbDatadeletion(){
  var mongoose = require('mongoose'); 
  var db1 = mongoose.createConnection('mongodb://localhost/CDB');

  db1.on('error', console.error.bind(console, 'connection error:')); 
  db1.once('open', function callback (){ 
    console.log('Database connectivity established...\n');
    var schema = new mongoose.Schema({ USER_ID:String, APIKEY:String, SERVICE_ORCH_NAME:String, COLLATED_DATA:String });
    var C_DATA_STORE = db1.model('C_DATA_STORE', schema);
    var query = C_DATA_STORE.find();

    console.log('Query Operation is being performed...');
    query.select('COLLATED_DATA SERVICE_ORCH_NAME');
    query.exec(function (err, data) {
      if (err) return handleError(err);
      console.log('Before Deletion');
      console.log(data[0]);
      db1.C_DATA_STORE.remove();
      console.log('After Deletion');
      console.log(data[0]);
    });
  });
}

当我运行上面的代码时,我无法删除我的collection.Below是错误消息

TypeError: Cannot call method 'remove' of undefined

任何想法如何解决这个问题都会非常有用

1 个答案:

答案 0 :(得分:2)

要从集合中删除所有文档:

C_DATA_STORE.remove(callback)

要删除集合,请使用驱动程序方法:

C_DATA_STORE.collection.drop(callback)

要删除数据库,请使用驱动程序方法:

db1.db.dropDatabase(callback)