Repository navigation
Expand file tree
/
Copy pathmongo.js
More file actions
63 lines (42 loc) · 1.48 KB
/
Copy pathmongo.js
File metadata and controls
63 lines (42 loc) · 1.48 KB
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
/*global process*/
const mongoose = require('mongoose')
if (process.argv.length < 3) {
console.log('the format is: node mongo.js password Anna 040-1234556')
process.exit(1)
}
if (process.argv.length === 5) {
const password = process.argv[2]
const url = `mongodb+srv://admina:${password}@cluster0.zeecn.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
const personSchema = new mongoose.Schema({
name: String,
number: String
})
const Person = mongoose.model('Person', personSchema)
const person = new Person({
name: process.argv[3],
number: process.argv[4]
})
person.save().then(response => {
// log to console "added name to number to phonebook"
console.log(`added ${response.name} number ${response.number} to phonebook`)
mongoose.connection.close()
})
}
else if (process.argv.length === 3) {
const password = process.argv[2]
const url = `mongodb+srv://admina:${password}@cluster0.zeecn.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true })
const personSchema = new mongoose.Schema({
name: String,
number: String
})
const Person = mongoose.model('Person', personSchema)
Person.find({}).then(result => {
console.log('phonebook:')
result.forEach(person => {
console.log(person.name, person.number)
})
mongoose.connection.close()
})
}