-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
38 lines (31 loc) · 890 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
36
37
38
const mongoose = require('mongoose')
// POISTA SALASANA ENNEN PUSHAUSTA:
const url = 'mongodb://testuser:---@ds131784.mlab.com:31784/fullstack-persons'
mongoose.connect(url)
const personSchema = new mongoose.Schema({
name: String,
number: String
})
const Person = mongoose.model('Person', personSchema);
if (process.argv.length > 2) {
const person = new Person({
name: process.argv[2],
number: process.argv[3]
})
person
.save()
.then(response => {
console.log(`lisätään henkilö ${person.name} numero ${person.number} luetteloon`)
mongoose.connection.close()
})
} else {
console.log('Puhelinluettelo')
Person
.find({})
.then(result => {
result.forEach(person => {
console.log(`${person.name} ${person.number}`)
})
mongoose.connection.close()
})
}