forked from mrhm-dev/full-stack-army
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (55 loc) · 1.23 KB
/
index.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
58
const mongoose = require('mongoose');
const personSchema = new mongoose.Schema({
firstName: {
type: String,
required: true,
minlength: [3, 'Minimum 3 chars'],
maxlength: [20, 'Maximum 20 chars'],
},
lastName: {
type: String,
required: true,
minlength: [3, 'Minimum 3 chars'],
maxlength: [20, 'Maximum 20 chars'],
},
email: {
type: String,
required: true,
validate: {
validator: function (v) {
return v.endsWith('.com');
},
message: 'Invalid email formats',
},
},
age: Number,
bio: String,
single: Boolean,
});
const Person = mongoose.model('Person', personSchema);
mongoose
.connect('mongodb://localhost:27017/mongo-demo')
.then(async () => {
console.log('Database connected');
const person = new Person({
firstName: 'Aditya',
lastName: 'Chakraborty',
email: '[email protected]',
age: 30,
bio: 'Backend Developer',
single: true,
});
await person.save();
console.log('Person created');
console.log(person);
// const people = await Person.find({ lastName: 'Chakraborty' });
// console.log(people);
// const person = new Person({ firstName: '11' });
// await person.save();
})
.catch((e) => {
console.log(e);
})
.finally(() => {
mongoose.connection.close();
});