课程: Databases for Node.js Developers (2021)

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Define your first schema and model

Define your first schema and model

- [Instructor] As we learned before Mongoose relies on schemas and models. And in this video, we will create a schema and a model for storing items. So as we see on this form, our item is represented by an SKU which is an item number, and name and the price. So let's head over to Visual Studio code and implement that. For that to go into models, so this directory exists already. In there, I click on item.js. As you see, this file is still empty and we will now start implementing the schema. So I start with adding Mongoose, const mongoose = require('mongoose') and then I start defining the schema. Const ItemSchema = mongoose.Schema. And in there we add now a configuration object with our schema definition. First we define the SKU. And the SKU as the type, and the type should be a number. So it should be numeric. It should be required, so at required true And I want it to be indexed. So there should be a database index…

内容