To use our poemSchema definition:

const poemSchema = new mongoose.Schema({ title: String, });

we need to convert our poemSchema into a Model we can work with. Schemas provide the definition for a model. A model maps to a collection in your MongoDB database.

Models are defined by passing a Schema instance to mongoose.model like this:

mongoose.model(modelName, schema):

The first argument is the singular name of the collection your model is for. The second argument is your previously created Schema. In the case of our poetry web app, turning our schema into a model would look like this:

const Poem = mongoose.model('Poem', poemSchema);

Models are constructors that we define based on our Schema. They represent documents which can be saved and retrieved from our database. All document creation and retrieval from the database is handled by these models.



Use mongoose.model() to create a model named MagicItem with the magicItemSchema Schema.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?