@@ -8,8 +8,21 @@ mongoose.connect('mongodb://localhost/myapp')
8
8
9
9
10
10
const courseSchema = new mongoose . Schema ( {
11
- name : { type : String , required : true , minlength : 5 , maxlength : 255 } ,
12
- category : { type : String , enum : [ 'web' , 'mobile' , 'network' ] } ,
11
+ name : {
12
+ type : String ,
13
+ required : true ,
14
+ minlength : 5 ,
15
+ maxlength : 255 ,
16
+ //match: /pattern/
17
+ } ,
18
+ category : {
19
+ type : String ,
20
+ required : true ,
21
+ enum : [ 'web' , 'mobile' , 'network' ] ,
22
+ lowercase : true ,
23
+ // uppercase: true,
24
+ trim : true //paddings around string removed
25
+ } ,
13
26
author : String ,
14
27
tags : {
15
28
type : Array ,
@@ -20,17 +33,24 @@ const courseSchema = new mongoose.Schema({
20
33
//filesystem etc async work
21
34
const result = v && v . length > 0
22
35
callback ( result )
23
- } , 4000 )
24
- return
36
+ } , 1000 )
25
37
} ,
26
38
message : 'A course should have at least one tag.'
27
39
}
28
40
} ,
29
- data : {
41
+ date : {
30
42
type : Date ,
31
43
default : Date . now
32
44
} ,
33
- isPublished : Boolean
45
+ isPublished : Boolean ,
46
+ price : {
47
+ type : Number ,
48
+ required : function ( ) { return this . isPublished } ,
49
+ min : 10 ,
50
+ max : 200 ,
51
+ get : v => Math . round ( v ) , //read
52
+ set : v => Math . round ( v ) //set
53
+ }
34
54
} )
35
55
36
56
const Course = mongoose . model ( 'Course' , courseSchema ) ;
@@ -39,22 +59,19 @@ const Course = mongoose.model('Course', courseSchema);
39
59
async function createCourse ( ) {
40
60
const course = new Course ( {
41
61
name : 'Angular Course' ,
42
- category : 'web ' ,
62
+ category : 'Web ' ,
43
63
author : 'Mosh' ,
44
- tags : [ 'node' , ' frontend'] ,
64
+ tags : [ 'frontend' ] ,
45
65
isPublished : true ,
46
- price : {
47
- required : function ( ) { return this . isPublished } ,
48
- min : 10 ,
49
- max : 200
50
- }
66
+ price : 15.8
51
67
} ) ;
52
68
//if is published then price is required
53
69
try {
54
70
const result = await course . save ( ) ;
55
- console . log ( result ) ;
71
+ console . log ( result , 1 ) ;
56
72
} catch ( ex ) {
57
- console . log ( ex . message )
73
+ for ( field in ex . errors )
74
+ console . log ( ex . errors [ field ] . message , 2 )
58
75
}
59
76
}
60
77
@@ -63,8 +80,11 @@ async function getCourses() {
63
80
const pageSize = 10 ;
64
81
65
82
const courses = await Course
66
- . find ( { author : 'Mosh' , isPublished : true } )
67
- . skip ( ( pageNumber - 1 ) * pageSize )
83
+ . find ( { _id : '5b45645d72013254f0d4fa6a' } )
84
+
85
+ // .find({ author: 'Mosh', isPublished: true })
86
+ // .skip((pageNumber -1) * pageSize)
87
+
68
88
// .find({ price: { $gte: 10, lte: 20 } })
69
89
// .find({ price: { $in: [10, 15, 20] } })
70
90
// .find()
@@ -79,15 +99,13 @@ async function getCourses() {
79
99
// //Contains mosh
80
100
//
81
101
// .find({ author: /.*Mosh.*/i })
82
- . limit ( pageSize )
102
+ // .limit(pageSize)
83
103
. sort ( { name : 1 } ) //1 indicates ascending order, -1 is descending
84
- // .select({ name: 1, tags: 1})
85
- . count ( )
86
- console . log ( courses )
104
+ . select ( { name : 1 , tags : 1 , price : 1 } )
105
+ // .count()
106
+ console . log ( courses [ 0 ] . price )
87
107
}
88
108
89
- getCourses ( ) ;
90
-
91
109
//QueryFirst
92
110
// async function updateCourse(id) {
93
111
// const course = await Course.findById(id);
@@ -132,10 +150,11 @@ updateCourse("5b3e2977e7cb2808d987bd7f")
132
150
// removeCourse("5b3e2977e7cb2808d987bd7f")
133
151
134
152
135
- async function removeCourse ( id ) {
136
- // const result = await Course.deleteMany({ _id: id })
137
- const course = await Course . findByIdAndRemove ( id )
138
- console . log ( course )
139
- }
140
-
141
- removeCourse ( "5b3e2977e7cb2808d987bd7f" )
153
+ // async function removeCourse(id) {
154
+ // // const result = await Course.deleteMany({ _id: id })
155
+ // const course = await Course.findByIdAndRemove(id)
156
+ // console.log(course)
157
+ // }
158
+ //
159
+ // removeCourse("5b3e2977e7cb2808d987bd7f")
160
+ getCourses ( ) ;
0 commit comments