-
Notifications
You must be signed in to change notification settings - Fork 0
/
databaseInit.js
58 lines (48 loc) · 1.45 KB
/
databaseInit.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
let itemNames = ["watermellon", "cat", "dog", "chessboard","water", "juice", "mustard", "sword", "books", "headsets"]
let items = [];
itemNames.forEach(name =>{
let u = {};
u.name = name;
u.numItems = 10;
u.cost = 10.00;
items.push(u);
});
let mongo = require("mongodb");
let MongoClient = mongo.MongoClient;
let db;
MongoClient.connect("mongodb://localhost:27017/", function(err,client){
if(err) throw err;
db = client.db("store");
db.listCollections().toArray(function (err,result){
if(result.length == 0){
db.collection("items").insertMany(items,function(err,result){
if(err){
throw err;
};
console.log(result.insertedCount + " items successfully added (should be 10)");
client.close()
});
return;
}
let numDropped = 0;
let toDrop = result.length;
result.forEach(collection =>{
db.collection(collection.name).drop(function (err,delOk){
if(err){
throw err;
}
console.log("Dropped Collection: " + collection.name);
numDropped++;
if (numDropped == toDrop){
db.collection("items").insertMany(items,function(err,result){
if(err){
throw err;
};
console.log(result.insertedCount + " items successfully added (should be 10)");
client.close()
});
}
})
})
})
})