-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongo.js
36 lines (30 loc) · 967 Bytes
/
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
const mongoose = require('mongoose')
if(process.argv.length < 3){
console.log('give password as an argument')
process.exit(1)
}
const password = process.argv[2]
const url = `mongodb+srv://ontheqt:${password}@cluster0-8xuqa.mongodb.net/phone-book-app?retryWrites=true&w=majority`
mongoose.connect(url, {useNewUrlParser: true})
const phonebookSchema = new mongoose.Schema({
name: String,
number: String,
})
const Phonebook = mongoose.model('Phonebook', phonebookSchema)
if(process.argv.length === 5){
const person = new Phonebook({
name: process.argv[3],
number: process.argv[4]
})
person.save().then(result => {
console.log(`added ${process.argv[3]} ${process.argv[4]}`)
mongoose.connection.close()
})
}
else{
Phonebook.find({})
.then(persons =>{
persons.map(person => console.log(`${person.name} ${person.number}`))
mongoose.connection.close()
})
}