Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Schema Types" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

Scott walks through a more complex schema definition to introduce all the different data types that can be used when defining properties. These data types include Boolean, String, Number, Date, and Buffer. Scott also talks about nested objects and additional validation rules like min and max.

Get Unlimited Access Now

Transcript from the "Schema Types" Lesson

[00:00:00]
>> [MUSIC]

[00:00:03]
>> Speaker 1: So, above is a small example of a schema with Mongoose, but those are the basics. Here's a tour of a whole bunch of other types we can use in a schema, this time we're gonna make a dog schema. All right, same thing, new Mongoose schema. Here we have a name for the dog, it's type is string, and it's also required.

[00:00:24] And we also want to enforce that it be unique. So there can be no two dogs with the same name. So if you try to save a dog with a name whose type was string but then there already existed one with the same name, with the same string, it will throw a validation error or a unique error.

[00:00:41] Unique also accounts for no values too, but because we put require true, we probably won't run into the error. You will have that error like, if you didn't put require true here but you put unique true and the first dog you save didn't have a name because its not required, the next dog you saved didn't have a name, it'll throw a validation error cuz you all ready have one dog who doesn't have a name.

[00:01:04] So you can't have another dog who doesn't have a name. They're the same thing. The absence of no name is the same thing. So they're not unique. You can get around that though by saying spars true and stuff like that. But by default it will say that. Another type is date.

[00:01:20] So when the dog was adopted, it's going to be a date. A good practice, too, is like, you can put defaults to. You can add a property here, just like we put these properties on this object, we can say defaults to date.now, or date.whenever, and put a date in there.

[00:01:40] Another type that we saw was boolean. So does it have it's shots, true or false? Another type is a buffer. If you've ever used node extensively with file systems, there exist things as buffers. Like when you read a file, right if I come in here, and I say, node and I say var fs equals require fs, and I say fs.readFileSync, and I wanna read the

[00:02:16]
>> Speaker 1: package
>> Speaker 1: .json. Alright I get this buffer. That's the type of buffer and if you just want to turn it to a string, you just say to string, then you get the actual file. So buffer is also a type, collar code, I couldn't think of why a dog would have a buffer, so maybe it has a hi-tech collar on, I don't know.

[00:02:40] But buffer, collar code, I don't know. Age, type is number. Of course we need numbers. You can also do validations here. It's min is 0, it's max is 30. You try to save a dog with a number not in that range, it will freak out. This is where it gets kinda interesting and you can see the flexibility of Mongo showing through Mongoose.

[00:03:04] Cuz again, Mongo doesn't care. Mongoose is trying to allow us to do what we want but still have some structure. But, you can start to see that shine through here is when we have an array. So, when you have this array you can do all types of things.

[00:03:15] So, when I say toys is type of array I can just put an empty array there and move on. And that's the
>> [INAUDIBLE]
>> Speaker 1: Bump the font up, yes. Is that good? Okay. So if I say toys is type of array, I can just move on just by putting an array literally right there.

[00:03:36] But, I can also blue print how the data in that arrays gonna look like as well. And it can be exactly like I've been doing up here, I have the same rules apply. So, I can say, you know what, I want toys to be an array of objects, and all those objects must have a name property on it, and the name must be unique, and they also must be this and that.

[00:04:01] You can do that inside an object. You can also put in another schema in here. So you can have nested documents. So you can be like, yeah, I have a toy schema somewhere else and that's what's gonna go inside of here. You can do that. You can also just tell Mongoose, it's just gonna be a type of mixed.

[00:04:17] So you can tell Mongoose, look at the types in a minute, like elicit types but you can say Mongoose I just want this to be a mixed bag of types. I don't know what it's gonna be. It might be a boolean. It might be an object. It might be a strain.

[00:04:29] I don't know. So it's gonna be a mixed bag or you can just leave it blank. So it's up to you. It all depends on how you want to query your data because, like I said, Mongo doesn't care. So because it doesn't care and Mongoose is giving you the flexibility to do whatever you want, it really all comes down to how are you gonna query your data.

[00:04:47] What do you need when you ask the dog? And that's how you know you want to put it in. There are database type things, like common things that you don't want to do. But for the most part, it's whatever you want to put in there. Nested objects looks like this.

[00:05:04] So if you want a nested object, you'd say the property and use the object literal, just like we did up here, like with the name, but this is not a nested object. This is just me defining the name. So up here I'm saying I have a property called Name.

[00:05:17] It's type is of String. It is also required. It is also unique. I'm using these object literal brackets, right? But down here, I'm also using the brackets, but you notice there's no type here. I'm not seeing type anywhere. I'm saying, state must be a string, city must be a string, zip must be a number.

[00:05:36] What I'm saying here is that location is an object, and it has a property on it called State, whose type is string. And a city property whose type is string. So it's a little different although it looks the same, and that's because I'm not putting a type right here.

[00:05:53] It's implied that the location is of type object because I'm adding new properties on it that aren't keywords in Mongoose. Like I didn't put the keyword required here, I didn't put the keyword unique here.