Replace stub page with new hints (#34799)
* Replace stub page * Update index.md
This commit is contained in:
@@ -1,24 +1,41 @@
|
||||
---
|
||||
title: Create a Model
|
||||
---
|
||||
## Create a Model
|
||||
|
||||
### Creating Schema
|
||||
There are 3 things to do in this challenge. You can click each item to see the code.
|
||||
|
||||
<details>
|
||||
<summary>Assign Mongoose Schema to a variable. This is not necessary but will make your code easier to read.</summary>
|
||||
|
||||
```javascript
|
||||
const Schema = mongoose.Schema;
|
||||
```
|
||||
</details>
|
||||
|
||||
See the [Mongoose docs](https://mongoosejs.com/docs/guide.html) first where is a lot of useful stuff.
|
||||
When you are building schema you can use either of three options for name validation
|
||||
```javascript
|
||||
```js
|
||||
name: String
|
||||
name: {type: String}
|
||||
name: {type: String, required: true} //preferred
|
||||
```
|
||||
For array of favoriteFoods here is the validation:
|
||||
```javascript
|
||||
favoriteFoods: [{ type: String }]
|
||||
```
|
||||
### Creating a Model
|
||||
Now that we have the schema of our model, we can actually create a model by:
|
||||
```javascript
|
||||
var Model = mongoose.model('Model', modelSchema);
|
||||
```
|
||||
|
||||
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
||||
<details>
|
||||
<summary>Create Person schema.</summary>
|
||||
|
||||
```javascript
|
||||
const personSchema = new Schema({
|
||||
name: { type: String, required: true },
|
||||
age: Number,
|
||||
favoriteFoods: [String]
|
||||
});
|
||||
```
|
||||
**Note**: If you choose to skip the first step, you have to use `mongoose.Schema` instead of `Schema`.
|
||||
</details>
|
||||
<details>
|
||||
<summary>Create Person model from the schema.</summary>
|
||||
|
||||
```javascript
|
||||
const Person = mongoose.model('Person', personSchema);
|
||||
```
|
||||
</details>
|
||||
|
Reference in New Issue
Block a user