The mongoose model

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

Introduction

MongoDB is referred to as a schemaless database, meaning the number and type of columns do not need to be specified before inserting data. However, because it is schema free, MongoDB does not provide support for creating schemas, validation or abstraction. Mongoose is an Object Modeling Library, or ODM, that supports the methods and operators provided by MongoDB. One of the main benefits of using Mongoose is its model class that is used to interact with MongoDB when working within environments like NodeJS. This tutorial will explain the Mongoose model class and its use with MongoDB.

Model

MongoDB is designed to store data in documents, but there is no schema definition in MongoDB documents. This is where the Mongoose model comes in as it is used to interact with MongoDB. The model is simply an instance of a document and the schema defines what each document will look like.

To begin, a schema must be created. For this example, suppose that the details of business’s employees are being stored in a database with each document containing the three fields of “name,” “age” and “location.” Refer to the following example:

1
2
3
4
5
{
    "name" : "John",
    "age" : 21,
    "location" : "New York"
}

In this example the “name” and “location” fields will accept only string values, whereas the “age” field will accept only numbers.

The first step in creating a schema is to import Mongoose into the file. Once imported, Mongoose will be used to call the schema() that will then in turn be used to create the schema. Here is an example:

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

const Schema = mongoose.Schema;

The schema is created as follows and will be called “Employee”.

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

const Schema = mongoose.Schema;

let Employee = new Schema({});

Now the schema must be passed to the schema constructor, as shown here:

1
2
3
4
5
6
7
8
9
10
11
{
    name: {
        type : String
        },
    age : {
        type : Number
        },
    location : {
        type : String
        }
}

Now add the schema to the main file:

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

const Schema = mongoose.Schema;

let Employee = new Schema({
  name: {
    type: String
  },
  age: {
    type: Number
  },
  location: {
    type: String
  }
});

The schema is now ready, however, it can’t be used until the model is created. Execute the following command to create the model:

1
let employeeModel = mongoose.model();

Currently, nothing is being passing to the Model(). Here the class has two arguments, with the first argument being the name of the collection that will be created for the documents and the second being the schema.

Here is an example:

1
let employeeModel = mongoose.model("Details", Employee);

Note that the name of the collection is “Details.” Add this to the main file as follows:

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

const Schema = mongoose.Schema;

let Employee = new Schema({
  name: {
    type: String
  },
  age: {
    type: Number
  },
  location: {
    type: String
  }
});

let employeeModel = mongoose.model("Details", Employee);

Now the Mongoose model has been successfully created. In the event it may be needed for other uses, export the model as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const mongoose = require("mongoose");

const Schema = mongoose.Schema;

let Employee = new Schema({
  name: {
    type: String
  },
  age: {
    type: Number
  },
  location: {
    type: String
  }
});

let employeeModel = mongoose.model("Details", Employee);

module.exports = employeeModel;

The exported Mongoose model is now ready for use in other applications.

Conclusion

This tutorial discussed the Mongoose model class and how to use it to interact with MongoDB. The tutorial specifically covered how to use Mongoose to create a schema, how to create the Mongoose model and how to import Mongoose into the main file. The article then explained the two class arguments for the Mongoose model and how to export the finished model for future use. Keep in mind that everything starts with a schema in Mongoose. In order to use a schema there must be a model class and the model is simply an instance of a document with the schema defining what the documents will look like. It is important to remember that while MongoDB stores data in documents, there is no schema definition in MongoDB documents and the Mongoose model is needed for these types of interactions with MongoDB.

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.