forked from ThorntonTomasetti/HealthyReentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset_status.js
63 lines (48 loc) · 1.63 KB
/
reset_status.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
51
52
53
54
55
56
57
58
59
60
61
62
63
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectId;
MongoClient.connect(process.env.MONGO_URL, {
useUnifiedTopology: true
}).then(function (db) {
console.log("CONNECTED TO DB");
resetUsersStatus(db);
}).catch(err => {
console.error("An error occurred reading the database.");
console.error(err);
});
function resetUsersStatus(client_db) {
let db = client_db.db();
// get all users
// for each user save status as green
let collection = db.collection('users');
let statusCollection = db.collection('status');
let allUsersCount = 0
let counter = 0;
collection.find().count().then(function (count) {
console.log("Total users count :", count);
allUsersCount = count;
var d = new Date();
const cursor = collection.find();
cursor.forEach(function (user) {
//https://docs.mongodb.com/manual/reference/method/db.collection.save/ use insertOne
statusCollection.insertOne({
user: ObjectId(user._id),
date: d,
status: 0
}).then(result => {
console.log(`Successfully inserted item with _id: ${result.insertedId}`);
isDone();
})
.catch(err => {
console.error(`Failed to insert item: ${err}`);
isDone();
});
});
});
function isDone() {
counter += 1;
if (allUsersCount === counter) {
console.log("Closing DB");
client_db.close();
}
}
}