-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
41 lines (36 loc) · 1.06 KB
/
mongo.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
const mongoose = require("mongoose");
// console.log(process.argv)
const v = process.argv;
const password = v[2];
const url = `mongodb+srv://chryrgm:${password}@cluster0.x3docjg.mongodb.net/phonebookApp?retryWrites=true&w=majority`;
mongoose.set("strictQuery", false);
mongoose.connect(url);
const entrySchema = new mongoose.Schema({
name: String,
number: Number,
});
const Entry = mongoose.model("Entry", entrySchema);
if (v.length === 3) {
console.log("phonebook:");
Entry.find({}).then((entries) => {
entries.forEach((entry) => {
console.log(`${entry.name} ${entry.number}`);
});
mongoose.connection.close();
});
} else if (v.length === 5) {
const entry = new Entry({
name: v[3],
number: v[4],
});
entry.save().then((result) => {
console.log(`added ${v[3]} number ${v[4]} to phonebook`);
mongoose.connection.close();
});
} else {
console.log("(to view phone book entries)--> node mongo.js yourpassword ");
console.log(
"(to add new entry)--> node mongo.js yourpassword Anna 040-1234556"
);
process.exit(1);
}