Mongoose Schemas in React

Kim McGrath
4 min readApr 11, 2019

--

What is a mongoose Schema, and how and why do I use one?

To begin, let’s have a quick chat about mongoose itself. Mongoose is a library that is used with node.js that makes using MongoDB easier. MongoDB is a hugely powerful and dynamic database resource — but with great power comes great responsibility. Mongoose helps us wield all that power by adding structure to the dynamic nature of MongoDB.

mongoose Models vs mongoose Schemas

A Schema is part of a Model in mongoose. Models are the rock band — it is how your API creates, queries, updates, and deletes from your database. Anything goes in the Model, there is no structure to the data.

A mongoose Schema is the drummer in the band — it defines the structure of the document, the default values, its validators and other things. The Schema makes using the data more usable.

The Schema is applied to the Model, and mongoose applies this to the MongoDB database.

Why would I use a Schema?

It makes it much easier to create usable and searchable database collections.

Very simply, it does this two ways:

  1. Defining the data field you want to collect and use:

2. For example, if you are collecting data about rock bands and for your purposes, you need all of the following information:

  • Band name
  • Number of albums
  • First tour date
  • Lead singer
  • Guitarist
  • Drummer
  • Band motto
  • Years active

You still have full flexibility to decide what data you want. Once you’ve decided what you want, you can:

  1. Enforce the method they are entered into the database.

This means you can make sure the number of albums is entered as a number, and that the lead singer’s name is a string, or that the years active is an array. This allows for further control over the data. A great example is that you can require the band name to be unique — this means there is no confusion should you need to use or amend the data at a later date.

{

  • Band name: { type: String, required: true, unique: true },
  • Number of albums: Number,
  • First tour date: Date,
  • Lead singer: String,
  • Guitarist: String,
  • Drummer: String,
  • Band motto: Mixed,
  • Years active: Array

}

Put together, it looks like this:

What’s in there that we haven’t covered yet?

const mongoose = require(“mongoose”); is how we are able to use the mongoose library.

const rockbandsSchema names the Schema

= new mongoose.Schema is the code required to make this is a mongoose Schema

module.exports = mongoose.model(“Rockband”, rockbandsSchema) is the code required so that it can be used elsewhere in your app

Where in my API do I create a Schema?

In the models folder.

If you are using a database in your API, you will have a models folder. In the folder there will be a .js file for each Schema, and it will look like the screenshot posted above. Our example would be called rockbands.js.

Where in my API do I use a Schema?

In the app.js file.

The app.js file is the hub of your API — it holds the middleware and the routes your API uses to make requests and handle receipt of data.

In that file, you require mongoose like you did in the rockband.js file and then you define the URL to your MongoDB collection — where, exactly, your API will go to look for the information you want.

Const mongoose = require(“mongoose”); is the code required to use the mongoose library.

mongoose.connect(URL) is the exact address of our

What’s that white line?? Where your password goes!

The nuts and bolts

To start using mongoose

  1. Make sure you have MongoDB and node.js installed on your machine.

In your command line:

2. npm start

This starts your node server.

3. npm i mongoose

This installs mongoose.

In your code editor:

4. Create as many Schemas as your want in separate files in the model folder.

5. Require mongoose in your model file and app.js

6. Connect to mongoose using the URL in your app.js file

In conclusion

Mongoose Schemas are an easier way of dealing with data.

Next in the series…

Fetch requests by Karen

--

--