2013-03-20 78 views
0

您好我performin基礎上,再寄一次刪除我的分貝我使用cron來設置時間和刪除db.Below是代碼收集不MongoDB中得到刪除

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 

不知道如何解決,這將是很有益的

+0

這是方法'db1.C_DATA_STORE'未定義。它不應該是'C_DATA_STORE.remove();'? – 2013-03-20 09:53:38

+0

我認爲這是一個JavaScript錯誤。看到C_DATA_STORE不爲空。警報(C_DATA_STORE) – 2013-03-20 09:55:58

+0

C_DATA_STORE.remove()不會刪除集合。而C_DATA_STORE不爲空 – 2013-03-20 10:43:49

回答

2

從集合中刪除所有的文檔:

C_DATA_STORE.remove(callback) 

要刪除的收集,使用驅動程序方法:

C_DATA_STORE.collection.drop(callback) 

要刪除數據庫,使用驅動程序方法:

db1.db.dropDatabase(callback)