DEV Community

Austin Brown
Austin Brown

Posted on

Mongoose: The Basics

What is Mongoose?

Mongoose is an object data modeling library (ODM) used for simplifying the interactions between a javascript application and a MongoDB database. You could think of it by comparing it to the way that jQuery simplifies making modifications to the DOM.

The Setup

First, you will need to make sure Mongoose is installed via your terminal, which can be done with the command npm install mongoose. You may want to include this in the dependencies for your package.json as well.

At the top of your file, you'll need to require mongoose like this:

const mongoose = require('mongoose');

And then the last step:

mongoose.connect('mongodb://localhost/dbname', { useNewUrlParser: true })
  .then(() => console.log('Connected to database'))
  .catch(() => console.error('Failed to connect to database'));

On the first line, localhost would be replaced with whatever IP address your MongoDB is hosted from, and dbname is the name of the database you want to use. If a database with this name does not already exist, one will be created under that name.

The useNewUrlParser: true option is simply for updating the built-in deprecated URL parser and is currently recommended for all instances of the use of mongoose.connect().

Since mongoose.connect() returns a promise, an optional .then() and .catch() can be used to display a success/error confirmation to the console as shown in the above example.


To set up a schema, you might use a block of code that looks like this.

const usersModel = new mongoose.Schema({
  username: { type: String, unique: true },
  password: String,

Here, new mongoose.Schema() is used by passing an object as an argument which has key-value pairs where the key names will define column names in your table, and values that specify the data type that the column will hold. Typically it would be set equal to a variable as shown above so that that variable can then be used to create and modify entries in the table with its many built-in methods. This variable is now referred to as a model, but I like to think of it as a table since that is essentially what it is, so I will be referring to it as a table.

Some of the most common methods include:
-create() - for creating new entries in the table
-find() - for making general queries to the table
-deleteOne()/deleteMany() - for deleting entries from the table
-update() - for updating values on an existing record in the table

All of these methods accept standard JavaScipt objects as arguments. Here is a simple example using the usersSchema from earlier:

   username: 'MyUserName',
   password: 'abcd1234',

This would then create an entry in the table that looks like this:
Alt Text

Like the mongoose.connect(), each of these methods will return a promise, so .then(), .catch() and so on can be chained on to handle the response from the query as needed. This is especially useful with a method like find(), since fetching data from an external source as such needs to be handled asynchronously.

   username: 'MyUserName',
   .then((response) => {
      return response;


Mongoose paired with MongoDB is a great duo for general purpose database handling in full-stack web development known as the MERN stack. Since the library is in JavaScript, it is ideal in the many instances where the entire stack is in JavaScript which allows for less time translating between different languages and less likelihood for bugs due to errors in translation.

Top comments (0)