A NodeJS mongoose Schema Example

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

In this tutorial we hope to show a NodeJS with mongoose schema example as a starting point for developers setting up this type of architecture.

MongoDB is one of the most popular NoSQL databases around. It is fairly easy to use and understand when compared with SQL databases. But it is a schemaless database. When working with NodeJS, we can use mongoose ODM to define a schema for a MongoDB collection. A mongoose schema defines the shape of documents inside a particular collection. In this article, we will discuss how to create a schema in a mongoose with the help of an example.

Creating schema

Let’s start by creating a file in which we will define the schema. Create a new file and name it model.js. We need a package called mongoose. So let’s install mongoose by running the following command. NOTE This uses the node package manager (npm) that is used with NodeJS to install packages and manage dependencies.

1
npm install mongoose

Now, open model.js and import the mongoose package into it. To do this we use the require command in NodeJS.

1
const mongoose = require("mongoose");

Now that we have mongoose imported into our file we can utilize all the functionality that it provides. We will first use it to call the Schema constructor. Let’s create an instance of a Schema constructor using mongoose.

1
2
3
const mongoose = require("mongoose");

const Schema = mongoose.Schema;

The instance of schema can now be used to define a schema. Let’s create it.

1
2
3
4
5
6
7
8
9
10
11
let smartphone = new Schema({
  name: {
    type: String
  },
  price: {
    type: Number
  },
  inStock: {
    type: Boolean
  }
});

Observe the above code. You can see there are three attributes – name, price, and inStock. The name attribute is of String type, price attribute is of Number type, and inStock attribute is of Boolean type. We can also make the code more concise in the following way.

1
2
3
4
5
let smartphone = new Schema({
  name: String,
  price: Number,
  inStock: Boolean
});

Following are the SchemaTypes that are allowed.

NOTE Most of these data types will be familiar to developers. If you’re not familiar with data types these just define the type of data you will store. For example a name will be a String because it is text. A price or age will be a Number. A Boolean is a true of false value.

  1. String
  2. Number
  3. Date
  4. Mixed
  5. Boolean
  6. ObjectId
  7. Buffer
  8. Array

This is how model.js looks as of now.

1
2
3
4
5
6
7
8
9
const mongoose = require("mongoose");

const Schema = mongoose.Schema;

let smartphone = new Schema({
  name: String,
  price: Number,
  inStock: Boolean
});

As of now, this is useless. To use this schema, we need to create a model for it.

Creating a model

Let’s create for the schema.

1
const model = mongoose.model("smartphones", smartphone);

The first parameter of the mongoose.model is the name of the collection that will contain the documents. The second parameter is the schema we defined earlier. We have the model now, all we need is to export it so it can be used elsewhere.

1
2
3
const model = mongoose.model("smartphones", smartphone);

module.exports = model;

This is how we create a schema in mongoose.

1
2
3
4
5
6
7
8
9
10
11
12
13
const mongoose = require("mongoose");

const Schema = mongoose.Schema;

let smartphone = new Schema({
  name: String,
  price: Number,
  inStock: Boolean
});

const model = mongoose.model("smartphones", smartphone);

module.exports = model;

Now this model is ready to be imported and used to create data following our schema in the smartphones collection. This is a common development pattern when using NodeJS and MongoDB. An ODM like mongoose makes managing your data more intuitive.

Conclusion

In this tutorial we showed you how to setup a schema and model with mongoose. If you’re using the architecture of NodeJS, MongoDB with mongoose, you’ll create these schemas at the beginning of every project so it’s best to get familiar with them.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.