/
runFunc.js
50 lines (33 loc) · 1.71 KB
/
runFunc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//one time functions
// base dependencies for script
var mongoose = require('mongoose')
, db = require('./accessDB')
, moment = require('moment'); //time library
db.startup(process.env.MONGOLAB_URI); // start the db connection
console.log("Running this one function function...........................................");
console.log("...........................................REMOVING OLD EVENTS...........................................");
var today = moment().subtract('days',1);
console.log("today minus 1 day: "+today.calendar());
var query = db.Event.remove({}, ['id','name', 'datetime.starttimestamp']);
//query.where(this.time != undefined && this.time != '' );
//query.where('name._keywords').in(['Yogi']);
//query.where('name._keywords').in(['Yogi','Teen']);
query.where('datetime.starttimestamp').lt(today);
query.exec(function(err, events) {
if (err){
console.log(err);
}
console.log("count", events.length);
if (events.length == 0){
console.log("no events found.");
console.log("*******CLOSING DB - SCRIPT SHOULD TERMINATE AS EXPECTED ******");
db.closeDB(); // <--- VERY IMPORTANT. MUST CLOSE DB WHEN FINISHED.
}
events.forEach(function(event, index, array){
console.log("this event: ", index, event.datetime.starttimestamp, event.name);
if (index >= events.length){
console.log("*******CLOSING DB - SCRIPT SHOULD TERMINATE AS EXPECTED ******");
db.closeDB(); // <--- VERY IMPORTANT. MUST CLOSE DB WHEN FINISHED.
}
});//end forEach
});//end query