-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
44 lines (34 loc) · 1.08 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
42
43
44
const mongoose = require('mongoose')
const { argv } = process
if (argv.length < 3) {
console.log('Please provide the password as an argument: node mongo.js <password>')
process.exit(1)
}
const [, , password] = argv
const url =
`mongodb+srv://dgrishajev:${password}@cluster0.peec3.mongodb.net/phonebook-app?retryWrites=true&w=majority`
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
const personSchema = new mongoose.Schema({
name: String,
number: String,
})
const Person = mongoose.model('Person', personSchema)
if (argv.length > 3) {
const [, , , name, number] = argv
const person = new Person({ name, number })
person
.save()
.then(() => {
console.log(`added ${name} number ${number} to phonebook`)
})
.catch(error => console.log(error))
.finally(() => mongoose.connection.close())
} else {
console.log('phonebook:')
Person
.find({})
.then(res => {
res.forEach(({ name, number }) => console.log(`${name} ${number}`))
mongoose.connection.close()
})
}