Section 26 – Lessons 353 to 355 – Complete 2020 Web Development Bootcamp

Lesson 353 – Data Validation with Mongoose

Mongoose has built in validation checks.

When you create a schema (like the fruitSchema we created in an earlier lesson) instead of having the value be just a data type you can add in validation. This is the fruitSchema created in an earlier lesson –

const fruitSchema = new mongoose.Schema ({
	name: String,
	rating: Number,
	review: String
});

Instead of having the “rating:” value be just the “Number” data type, you can add in validation using curly braces as shown in the example below –

const fruitSchema = new mongoose.Schema ({
	name: String,
	rating: {
		type: Number,
		min: 1,
		max: 10
	},
	review: String
});

In the above example, the “rating:” value of “Number” has been replaced with the validators inside the curly braces. The “type:” adds back the “Number” value. The “min:” and “max:” set the minimum and maximum numbers that can be used for giving a rating. So if a rating of 12 is given, the “max: 10” validator will prevent that data being added to the database.

The challenge in this lesson is to make the “name:” field in fruitSchema required.

To complete this challenge, you have to change “name: String,” to –

name: {
  type: String,
  required: [true, "Please check your data entry, no name specified!"]
	},

The new fruitSchema with “name:” being required looks like this –

const fruitSchema = new mongoose.Schema ({
  name: {
    type: String,
    required: [true, "Please check your data entry, no name 
    specified!"]
	},
  rating: {
    type: Number,
    min: 1,
    max: 10
	},
  review: String
});

Lesson 354 – Updating and Deleting Data using Mongoose

If you added a Peach record without providing a review, for example, and wanted to update the database to include a review for the Peach record, you can use the .updateOne() method. The code to update the Peach record is –

Fruit.updateOne({_id: "5e94636a1d95454f21915522"}, {review: "Peaches are so yummy!"}, function(err) {
	if (err) {
		console.log(err);
	} else {
		console.log("Successfully updated the document.");
	}
});

To delete a record, you can use the .deleteOne() method.

The challenge in this lesson is to use the Mongoose documentation to figure out how to delete the Peach record.

This is the code you can use to delete the Peach record –

Fruit.deleteOne({_id: "5e9463daef4cfe4f27a1e860"}, function(err) {
	if (err) {
		cosole.log(err);
	} else {
		console.log("Successfully deleted the document.");
	}
});

The next challenge is to use the .deleteMany() method to delete all the records in the people collection.

The solution to this challenge is –

Person.deleteMany({name: "John"}, function(err) {
	if (err) {
		console.log(err);
	} else {
		console.log("Successfully deleted documents in People collection.");
	}
});

Lesson 355 – Establishing Relationships and Embedding Documents using Mongoose

In this lesson, you establish a relationship between the personSchema and the fruitSchema.

The first step in doing this is to refactor the personSchema to add a “favouriteFruit” whose value is “fruitSchema”. The code of the refactored personSchema is –

const personSchema = new mongoose.Schema ({
	name: String,
	age: Number,
	favouriteFruit: fruitSchema
});

The next step is to add a new fruit –

const pineapple = new Fruit ({
	name: "Pineapple",
	rating: 9,
	review: "Great fruit."
});

You then save it by calling –

pineapple.save();

You then add the pineapple as an embedded document in a new person like this –

onst person = new Person ({
	name: "Amy",
	age: 12,
	favouriteFruit: pineapple
});

The next step is to call –

person.save();

The challenge is to create a new fruit, save that fruit to the fruits collection and assign that fruit as a favourite fruit for John in the people collection.

The first step is to create and save a new fruit –

const mango = new Fruit ({
	name: "Mango",
	rating: 10,
	review: "So good!."
});

mango.save();

You then update the person “John” using the .updateOne() method –

Person.updateOne({name: "John"}, {favouriteFruit: mango}, function(err) {
	if (err) {
		console.log(err);
	} else {
		console.log("Successfully updated the John document to add a favourite fruit.");
	}
});