Skip to content

Commit 343b78c

Browse files
committed
Schema type options.
1 parent 1bbbce8 commit 343b78c

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

mongodb/mongo-demo/index.js

+49-30
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,21 @@ mongoose.connect('mongodb://localhost/myapp')
88

99

1010
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+
},
1326
author: String,
1427
tags: {
1528
type: Array,
@@ -20,17 +33,24 @@ const courseSchema = new mongoose.Schema({
2033
//filesystem etc async work
2134
const result = v && v.length > 0
2235
callback(result)
23-
}, 4000)
24-
return
36+
}, 1000)
2537
},
2638
message: 'A course should have at least one tag.'
2739
}
2840
},
29-
data: {
41+
date: {
3042
type: Date,
3143
default: Date.now
3244
},
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+
}
3454
})
3555

3656
const Course = mongoose.model('Course', courseSchema);
@@ -39,22 +59,19 @@ const Course = mongoose.model('Course', courseSchema);
3959
async function createCourse() {
4060
const course = new Course({
4161
name: 'Angular Course',
42-
category: 'web',
62+
category: 'Web',
4363
author: 'Mosh',
44-
tags: ['node', 'frontend'],
64+
tags: ['frontend'],
4565
isPublished: true,
46-
price: {
47-
required: function() { return this.isPublished },
48-
min: 10,
49-
max: 200
50-
}
66+
price: 15.8
5167
});
5268
//if is published then price is required
5369
try {
5470
const result = await course.save();
55-
console.log(result);
71+
console.log(result, 1);
5672
} catch (ex) {
57-
console.log(ex.message)
73+
for (field in ex.errors)
74+
console.log(ex.errors[field].message, 2)
5875
}
5976
}
6077

@@ -63,8 +80,11 @@ async function getCourses() {
6380
const pageSize = 10;
6481

6582
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+
6888
// .find({ price: { $gte: 10, lte: 20 } })
6989
// .find({ price: { $in: [10, 15, 20] } })
7090
// .find()
@@ -79,15 +99,13 @@ async function getCourses() {
7999
// //Contains mosh
80100
//
81101
// .find({ author: /.*Mosh.*/i })
82-
.limit(pageSize)
102+
// .limit(pageSize)
83103
.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)
87107
}
88108

89-
getCourses();
90-
91109
//QueryFirst
92110
// async function updateCourse(id) {
93111
// const course = await Course.findById(id);
@@ -132,10 +150,11 @@ updateCourse("5b3e2977e7cb2808d987bd7f")
132150
// removeCourse("5b3e2977e7cb2808d987bd7f")
133151

134152

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

Comments
 (0)