-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
38 lines (32 loc) · 850 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')
// korvaa url oman tietokantasi urlilla. ethän laita salasanaa Gothubiin!
const url = process.env.MONGODB_URI
mongoose.connect(url)
const Person = mongoose.model('Person', {
name: String,
number: String,
id: Number
})
if (process.argv.length == 2) {
Person
.find({})
.then(result => {
console.log("puhelinluettelo:")
result.forEach(person => {
console.log(person.name, person.number)
})
mongoose.connection.close()
})
} else if (process.argv.length == 4) {
const person = new Person({
name: process.argv[2],
number: process.argv[3],
id: Number((Math.random()*10000000).toFixed(0))
})
person
.save()
.then(response => {
console.log(`lisätään henkilö ${person.name} numero ${person.number} luetteloon`)
mongoose.connection.close()
})
}