Integrating MongoDB with Mongoose

Introduction

Hello there! Today, we're diving into the world of databases. We'll learn how to use MongoDB, a popular NoSQL database, with Mongoose, an elegant Object Data Modeling (ODM) solution for MongoDB and Node.js. By the end of this lesson, you will understand why MongoDB and Mongoose are essential tools in backend development and how to harness their power to store and manage data for your application.

Exploring MongoDB

Let's start our journey with a brief introduction to MongoDB. It is a database program that is designed to handle large amounts of data across many servers. It falls under the NoSQL category, meaning it does not rely on the traditional table-based format that SQL databases use. Instead, MongoDB holds data in a flexible, JSON-like format known as BSON (Binary JSON).

In MongoDB, data is organized into documents and collections, similar to how we would organize data in objects and arrays in JavaScript. These collections are then stored in databases. This structure can handle all types of data, making MongoDB a versatile choice for many different types of applications.

For instance, a MongoDB document for a to-do list item might look like this:

{
  "_id" : ObjectId("543d9f85c1ffa014dcfa648c"),
  "task" : "Do the dishes",
  "status": false
}

This is the equivalent of a row in an SQL database.

Understanding Mongoose

Mongoose is an ODM that provides a solution to work in an asynchronous environment. It serves as a layer of abstraction, allowing us to interact with MongoDB through our application using JavaScript.

Using Mongoose, we can define objects with strongly typed schemas that map directly to a MongoDB document. These objects, called models, contain all the functions and hooks we need to query and modify the data in our database.

By defining a schema and model for our to-do list items, we can create new items, update or delete existing ones, retrieve items with specific properties, and more, all using simple JavaScript.

Let's take a look at an example of a Mongoose schema and model:

const mongoose = require('mongoose');

const TodoSchema = new mongoose.Schema({
  task: {
    type: String,
    required: true
  },
  status: {
    type: Boolean,
    default: true
  }
});

const Todo = mongoose.model('Todo', TodoSchema);

module.exports = Todo;

In Mongoose, you create a schema using mongoose.Schema, which defines the structure of your documents. For example, you can define fields such as task, which holds the task name, and status, which indicates its current state.

Each field in the schema can have:

  • type: Specifies the expected data type, ensuring data consistency.
  • required: Enforces that a value must be provided before saving.
  • default: Assigns a default value if none is provided during creation.

Once the schema is defined, you create a model using mongoose.model. This model represents a collection and provides built-in methods for creating, reading, updating, and deleting documents.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal