-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
57 lines (48 loc) · 1.36 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
45
46
47
48
49
50
51
52
53
54
55
56
57
const mongoose = require('mongoose');
if (process.argv.length<3) {
console.log('Please provide the password as an argument: node mongo.js <password>');
process.exit(1);
}
const password = process.argv[2];
const personName = process.argv[3];
const personNumber = process.argv[4];
const personSex = process.argv[5];
const url =
`mongodb+srv://fullstack:${password}@cluster0.j0ypd.mongodb.net/phonebook-app?retryWrites=true&w=majority`;
// 连接数据库
mongoose
.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// 定义数据的 schema 和 model
const personSchema = new mongoose.Schema({
name: String,
number: Number,
sex: String,
});
const Person = mongoose.model('Person', personSchema);
// 从 process.argv 变量中获取参数,生成新的 person 数据
const newPerson = new Person({
name: personName,
number: personNumber,
sex: personSex,
});
// 添加到数据库
if (process.argv.length === 6) {
newPerson
.save()
.then(result => {
console.log(`added ${result.name} ${result.number} ${result.sex} to phonebook.`);
mongoose.connection.close();
})
}
if (process.argv.length === 3) {
Person
.find({})
.then(result => {
console.log(`phonebook contains:`);
result.forEach(person => {console.log(`${person.name} ${person.number}`)});
mongoose.connection.close();
})
}