-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseEx.js
104 lines (96 loc) · 3.07 KB
/
databaseEx.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
let prompt = require('prompt-sync')();
let mongoClient = require('mongodb').MongoClient , assert = require('assert');
let url = 'mongodb://ddematheu:[email protected]:21955/acmtest?authMexhanism=DEFAULT';
var command = "";
getcommand();
function ProcessInput()
{
if(command == "INSERT")
{
let title = prompt('Enter the title ');
let year = prompt('Enter the year ');
let author = prompt('Enter the author ');
let publisher = prompt('Enter the publisher ')
mongoClient.connect(url,function(err,db){
assert.equal(null,err);
db.collection('BookCollection').insertOne({
"Title":title,
"Year":year,
"Author":author,
"Publisher":publisher
}, function(err, item){
if(err) throw err;
console.log("Item was added");
command = getcommand();
});
db.close();
});
}
else if(command == "FIND")
{
let author = prompt('Enter the author of the book: ');
mongoClient.connect(url,function(err,db){
assert.equal(null,err);
db.collection('BookCollection').findOne({"Author":author}, function(err, item){
if(err) throw err;
console.log(item.Title);
console.log(item.Author);
console.log(item.Year);
console.log(item.Publisher);
command = getcommand()
});
db.close();
});
}
else if(command == "PRINT")
{
//Getting student info and requirements
mongoClient.connect(url,function(err,db){
assert.equal(null,err);
console.log("Connected correctly to server");
console.log("Printing Collection");
var books = db.collection('BookCollection');
books.find().toArray(function(e,d){
console.log(d);
command = getcommand()
});
db.close();
});
}
else if(command == "DELETE")
{
let title = prompt('Enter the title of the book: ');
mongoClient.connect(url,function(err,db){
assert.equal(null,err);
db.collection('BookCollection').deleteOne({"Title":title}, function(err, item){
if(err) throw err;
console.log("Item deleted");
command = getcommand()
});
db.close();;
});
}
else
{
console.log("Command not found");
command = getcommand();
}
}
function printAll()
{
//Getting student info and requirements
mongoClient.connect(url,function(err,db){
assert.equal(null,err);
console.log("Connected correctly to server");
var books = db.collection('BookCollection');
books.find().toArray(function(e,d){
console.log(d);
});
db.close();
});
}
function getcommand()
{
command = prompt('Enter Your Command (FIND,INSERT, DELETE, PRINT): ');
ProcessInput();
}